[PHP] Mailform Klasse mit generiertem Inhalt

Dieses Thema im Forum "Webentwicklung" wurde erstellt von Smokers, 22. März 2009 .

Schlagworte:
  1. Diese Seite verwendet Cookies. Wenn du dich weiterhin auf dieser Seite aufhältst, akzeptierst du unseren Einsatz von Cookies. Weitere Informationen
  1. #1 22. März 2009
    Mailform Klasse mit generiertem Inhalt

    Aaaalso , ich weis is nix soo besonderes, woltle es aber mal selber schreiben und habs auch soweit hinbekommen

    hier erstmal die klasse:

    PHP:
    <? php


    class  MailForm  {
        private 
    $s_recipient ;
        private 
    $s_websitename ;
        private 
    $s_formname  'mail_form' ;
        private 
    $a_values  = array();
        private 
    $s_form_elements ;
        private 
    $s_form ;
        public  
    $s_submit_value  'Absenden' ;
        public  
    $s_submit_name  'sendform' ;
        public  
    $error ;

        function 
    __construct ( $s_email , $s_websitename ) {
            if(
    $this -> checkType ( $s_email , 'email' , 1 )) {
                
    $this -> s_recipient  $s_email ;
                return 
    true ;
            }
            else {
                echo 
    'No valid email is given....' ;
                return 
    false ;
            }
            
    $this -> s_websitename  $s_websitename ;

        }
        public function 
    addFormElement ( $s_description , $s_name , $s_type , $i_need  1 ) {
            
    $this -> a_values [] = array ( $s_description , $s_name , $s_type , $i_need );
        }
        private function 
    checkType ( $s_val , $s_type , $i_need ) {
            
    #echo "testing : $s_val for $s_type required: $i_need <br>";
            
    switch ( $s_type ) {
                case 
    'email'         : if( $i_need  ==  &&  $s_val  ==  "" ) { return  true ; } else { if(( ereg ( "^[_a-zA-Z0-9-]+(\.[_a-zA-Z0-9-]+)*@([a-zA-Z0-9-]+\.)+([a-zA-Z]{2,3})$" $s_val ) &&  $s_val  !=  ""  &&  strlen ( $s_val ) >=  &&  strlen ( $s_val ) <=  30 )) { return  true ; } else { return  false ; } ;} ; break;
                case 
    'number'        : if( $i_need  ==  &&  $s_val  ==  "" ) { return  true ; } else { if( strlen ((int) $s_val ) >  ) { return  true ; } else { return  false ; } ;} ; break;
                case 
    'name'          : if( $i_need  ==  &&  $s_val  ==  "" ) { return  true ; } else { if( preg_match ( "/^[a-zäüö][a-zäüöß\s_-]+[a-zöüß]/i" , $s_val ) &&  strlen ( $s_val ) >  6 ) { return  true ; } else { return  false ; } ;} ; break;
                case 
    'textarea'      : if( $i_need  ==  &&  $s_val  ==  "" ) { return  true ; } else {  $s_val  strip_tags ( $s_val ); if( strlen ( $s_val ) >=  30  &&  strlen ( $s_val ) <  600  ) { return  true ; } else { return  false ; } ;} ; break;
                case 
    'select'        : if( $i_need  ==  &&  $s_val  ==  "" ) { return  true ; } else { if((int) $s_val  !=  "" ) { return  true ; } else { return  false ; } ;} ; break;
                case 
    'website'       : if( $i_need  ==  &&  $s_val  ==  "" ) { return  true ; } else { if( preg_match ( "#^(http|https)+(://www.)+([a-z0-9-_.]{2,}\.[a-z]{2,4})$#i" , $s_val )) { return  true ; } else { return  false ; } ;} ; break;
                default             : return 
    false ; break;
            }
        }
        private function 
    prepareForm () {
            foreach (
    $this -> a_values  as  $arg  =>  $val ){
                
    $this -> s_form_elements .=  '<tr>' . "\n" ;
                if(
    $val [ 2 ] !=  "select"  &&  $val [ 2 ] !=  "textarea" ) {
                    
    $this -> s_form_elements .=  '<td>' . $val [ 0 ];
                    if(
    $val [ 3 ] ==  1 ) {
                        
    $this -> s_form_elements .=  '*' ;
                    }
                    
    $this -> s_form_elements .=  '</td><td><input type="text" name="' . $val [ 1 ]. '" value="' . $_POST [ $val [ 1 ]]. '" /></td>' . "\n" ;
                }
                if(
    $val [ 2 ] ==  "textarea" ){
                    
    $this -> s_form_elements .=  '<td>' $val [ 0 ];
                    if(
    $val [ 3 ] ==  1 ) {
                        
    $this -> s_form_elements .=  '*' ;
                    }
                    
    $this -> s_form_elements .=  '</td>' . "\n" ;
                    
    $this -> s_form_elements .=  '<td><textarea name="' . $val [ 1 ] . '">' . $_POST [ $val [ 1 ]]. '</textarea></td>' . "\n" ;
                }
                if(
    $val [ 2 ] ==  'select' ) {
                    
    $this -> s_form_elements .=  '<td>' . $val [ 0 ];
                    if(
    $val [ 3 ] ==  1 ) {
                        
    $this -> s_form_elements .=  '*' ;
                    }
                    
    $this -> s_form_elements .=  '</td><td><select name="' . $val [ 1 ]. '">' . "\n" ;
                    
    $this -> s_form_elements .=  '<option value=""></option>' . "\n" ;

                    
    $frm_name  $val [ 1 ];
                    foreach (
    $val [ 0 ] as  $opt  =>  $desc ) {
                        
    $optn  $opt + 1 ;
                        if(
    $_POST [ $frm_name ] == (int)  $optn ) {
                            
    $this -> s_form_elements .=  '<option selected="selected" value="' . $optn . '">' . $desc . '</option>' . "\n" ;
                        }else {
                            
    $this -> s_form_elements .=  '<option value="' . $optn . '">' . $desc . '</option>' . "\n" ;
                        }

                    }
                    
    $this -> s_form_elements .=  '</select></td>' . "\n" ;

                }
                
    $this -> s_form_elements .=  '</tr>' . "\n" ;
            }
        }
        public function 
    buildForm () {
            
    $this -> prepareForm ();
            
    $this -> s_form .=  '<form action="' . htmlspecialchars ( $_SERVER [ 'PHP_SELF' ]). '" name="' . $this -> s_formname . '" method="POST">' . "\n" ;
            
    $this -> s_form .=  '<table name="' . $this -> s_formname . '">' . "\n" ;
            
    $this -> s_form .=  $this -> s_form_elements ;
            
    $this -> s_form .=  '</table>' . "\n" ;
            
    $this -> s_form .=  '<input type="submit" name="' . $this -> s_submit_name . '" value="' . $this -> s_submit_value . '"/>' . "\n" ;
            
    $this -> s_form .=  '</form>' . "\n" ;
            echo 
    $this -> s_form ;
        }
        private function 
    checkInputs () {
            foreach (
    $this -> a_values  as  $key  =>  $value ){
                if(!
    $this -> checkType ( $_POST [ $value [ 1 ]], $value [ 2 ], $value [ 3 ])) {
                    
    $this -> error .=  'Das Feld "' . $value [ 0 ]. '" enthält Fehler oder ist nicht vollständig ausgefüllt.'  . "<br />\n" ;
                }
            }
            if(
    $this -> error  ==  "" ) {
                
    $this -> sendMail ();
            }
            else {
                echo 
    $this -> error ;
            }
        }
        public function 
    checkUserData () {
            if(
    $_POST [ $this -> s_submit_name ] ==  $this -> s_submit_value ) {
                
    $this -> checkInputs ();
            }
        }
        private function 
    prepareMessage () {
            
    $message .=  "Hallo \neine neue Nachricht vom  $this -> s_websitename  Kontakformular.\n" ;
            
    $message .=  "Der Benutzer mit der IP: "  $_SERVER [ 'REMOTE_ADDR' ] . " schickte folgende Nachricht:\n\n" ;
            foreach (
    $this -> a_values  as  $key  =>  $value ) {
                if(
    $_POST [ $value [ 1 ]] !=  ""  &&  $value [ 2 ] !=  'select'  ) {
                    
    $message .=  $value [ 0 ] . "\t\t "  . $_POST [ $value [ 1 ]]. "\n" ;
                }
                if(
    $_POST [ $value [ 1 ]] !=  ""  &&  $value [ 2 ] ==  'select'  ) {
                    
    $optn  $_POST [ $value [ 1 ]] - 1 ;
                    
    $message .=  $value [ 0 ] . "\t\t "  . $value [ 0 ][ $optn ]. "\n" ;
                }
            }
            
    $message .=  "\nAuf bald.....\nDein Mailformular." ;
            return 
    $message ;
        }
        private function 
    getSubject () {
            foreach (
    $this -> a_values  as  $key  =>  $value ){
                if(
    $value [ 1 ] ==  'subject' ) {
                    
    $subject  $value [ 0 ];
                }
            }
            if(
    $subject  ==  "" ) {
                
    $subject  'Kein Betreff' ;
            }
            return 
    $subject ;
        }
        private function 
    sendMail () {
            
    $subject  $this -> getSubject ();
            
    $message  $this -> prepareMessage ();

            
    #echo "Message $message <br>";


            #$proof = @mail($this->s_recipient,$subject,$message,"FROM: info@$this->s_websitename");
            
    if( $proof ) {
                
    $this -> blockTemporary ();
            }
            else {
                
    "Aufgrund eines technischen Fehlers bitten wir Sie die Eingabe später erneut zu tätigen.\n" ;
            }
        }
    }


    ?>

    und der aufruf:
    PHP:
    <? php
    include_once( 'class.MailForm.php' );

    $form  = new  MailForm ( 'XXXXXXXXXXXXX@gmx.de' , 'XXXXXXXXXXX.de' );

    $form -> addFormElement ( 'Name,Vorname' , 'name' , 'name' );
    $form -> addFormElement ( '@Mail' , 'email' , 'email' );
    $form -> addFormElement ( 'Telefonnummer' , 'nummer' , 'number' );
    $form -> addFormElement (array( 'Allgemein' , 'Technisch' , 'Privat' ), 'subject' , 'select' );
    $form -> addFormElement ( 'Nachricht' , 'body' , 'textarea' );


    $form -> checkUserData ();
    $form -> buildForm ();
    ?>

    Soooo 2 Sachen hab ich mal =)



    1. Ich habe das problem wenn ich dieses array übergebe was selects erstellt, das ich die var die für die Labels zuständig ist nicht mehr nehmen kann da es ein array ist was die <option> des selects enthält.

    Sprich bei dem obigen beispiel steht statt eine Beschriftung "Array" , is ja logisch ^^
    nu aber meine Frage ob ich da auf ne simple art und weise doch noch ne beschriftung ausgeben kann bzw eine setzen kann ...
    (hoffe ihr wisst was ich meine ^^ )



    2. andere sicherheitsmängel oder features etc pp .....
     

  2. Anzeige
    Dealz: stark reduzierte Angebote finden.
  3. #2 22. März 2009
    AW: Mailform Klasse mit generiertem Inhalt

    Dein eMail Ding funktioniert nicht bei .info Domains. strlen(info)==4, du akzeptierst nur {2,3}. .mobi etc geht natürlich auch nicht. Gibt übrigens auch Domains mit Umlauten.

    Private Funktionen + Attribute kriegen ein _ vor dem Namen.
    PHP:
    <? php
    class  ohai  {
      private 
    $_privateVariable  'nooo' ;
      
      private function 
    _doPrivateStuff ( $privateData ) {
        
    // do random stuff.
      
    }
    }
    ?>
    - einfach damit man das besser unterscheiden kann, was privat und was public ist :p
     
  4. #3 22. März 2009
    AW: Mailform Klasse mit generiertem Inhalt

    PHP:
    <? php


    class  MailForm  {
        private 
    $_s_recipient ;
        private 
    $_s_websitename ;
        private 
    $_s_formname  'mail_form' ;
        private 
    $_a_values  = array();
        private 
    $_s_form_elements ;
        private 
    $_s_form ;
        public  
    $s_submit_value  'Absenden' ;
        public  
    $s_submit_name  'sendform' ;
        private 
    $_i_reposttime  600 ;
        public  
    $error ;

        function 
    __construct ( $s_email , $_s_websitename ) {
            
    session_start ();
            if(
    $this -> _checkType ( $s_email , 'email' , 1 )) {
                
    $this -> _s_recipient  $s_email ;
                return 
    true ;
            }
            else {
                echo 
    'No valid email is given....' ;
                return 
    false ;
            }
            
    $this -> _s_websitename  $_s_websitename ;

        }
        public function 
    setReposteTime ( $time ) {
            
    $this -> _i_reposttime  $time ;
        }
        public function 
    addFormElement ( $s_description , $s_name , $s_type , $i_need  1 ) {
            
    $this -> _a_values [] = array ( $s_description , $s_name , $s_type , $i_need );
        }
        private function 
    _checkType ( $s_val , $s_type , $i_need ) {
            
    #echo "testing : $s_val for $s_type required: $i_need <br>";
            
    switch ( $s_type ) {
                case 
    'email'         : if( $i_need  ==  &&  $s_val  ==  "" ) { return  true ; } else { if(( ereg ( "^[_a-zA-Z0-9-]+(\.[_a-zA-Z0-9-]+)*@([a-zA-Z0-9-]+\.)+([a-zA-Z]{2,4})$" $s_val ) &&  $s_val  !=  ""  &&  strlen ( $s_val ) >=  &&  strlen ( $s_val ) <=  30 )) { return  true ; } else { return  false ; } ;} ; break;
                case 
    'number'        : if( $i_need  ==  &&  $s_val  ==  "" ) { return  true ; } else { if( strlen ((int) $s_val ) >  ) { return  true ; } else { return  false ; } ;} ; break;
                case 
    'name'          : if( $i_need  ==  &&  $s_val  ==  "" ) { return  true ; } else { if( preg_match ( "/^[a-zäüö][a-zäüöß\s_-]+[a-zöüß]/i" , $s_val ) &&  strlen ( $s_val ) >  6 ) { return  true ; } else { return  false ; } ;} ; break;
                case 
    'textarea'      : if( $i_need  ==  &&  $s_val  ==  "" ) { return  true ; } else {  $s_val  strip_tags ( $s_val ); if( strlen ( $s_val ) >=  30  &&  strlen ( $s_val ) <  600  ) { return  true ; } else { return  false ; } ;} ; break;
                case 
    'select'        : if( $i_need  ==  &&  $s_val  ==  "" ) { return  true ; } else { if((int) $s_val  !=  "" ) { return  true ; } else { return  false ; } ;} ; break;
                case 
    'website'       : if( $i_need  ==  &&  $s_val  ==  "" ) { return  true ; } else { if( preg_match ( "#^(http|https)+(://www.)+([a-z0-9-_.]{2,}\.[a-z]{2,4})$#i" , $s_val )) { return  true ; } else { return  false ; } ;} ; break;
                default             : return 
    false ; break;
            }
        }
        private function 
    _prepareForm () {
            foreach (
    $this -> _a_values  as  $arg  =>  $val ){
                
    $this -> _s_form_elements .=  '<tr>' . "\n" ;
                if(
    $val [ 2 ] !=  "select"  &&  $val [ 2 ] !=  "textarea" ) {
                    
    $this -> _s_form_elements .=  '<td>' . $val [ 0 ];
                    if(
    $val [ 3 ] ==  1 ) {
                        
    $this -> _s_form_elements .=  '*' ;
                    }
                    
    $this -> _s_form_elements .=  '</td><td><input type="text" name="' . $val [ 1 ]. '" value="' . $_POST [ $val [ 1 ]]. '" /></td>' . "\n" ;
                }
                if(
    $val [ 2 ] ==  "textarea" ){
                    
    $this -> _s_form_elements .=  '<td>' $val [ 0 ];
                    if(
    $val [ 3 ] ==  1 ) {
                        
    $this -> _s_form_elements .=  '*' ;
                    }
                    
    $this -> _s_form_elements .=  '</td>' . "\n" ;
                    
    $this -> _s_form_elements .=  '<td><textarea name="' . $val [ 1 ] . '">' . $_POST [ $val [ 1 ]]. '</textarea></td>' . "\n" ;
                }
                if(
    $val [ 2 ] ==  'select' ) {
                    
    $this -> _s_form_elements .=  '<td>' . $val [ 0 ];
                    if(
    $val [ 3 ] ==  1 ) {
                        
    $this -> _s_form_elements .=  '*' ;
                    }
                    
    $this -> _s_form_elements .=  '</td><td><select name="' . $val [ 1 ]. '">' . "\n" ;
                    
    $this -> _s_form_elements .=  '<option value=""></option>' . "\n" ;

                    
    $frm_name  $val [ 1 ];
                    foreach (
    $val [ 0 ] as  $opt  =>  $desc ) {
                        
    $optn  $opt + 1 ;
                        if(
    $_POST [ $frm_name ] == (int)  $optn ) {
                            
    $this -> _s_form_elements .=  '<option selected="selected" value="' . $optn . '">' . $desc . '</option>' . "\n" ;
                        }else {
                            
    $this -> _s_form_elements .=  '<option value="' . $optn . '">' . $desc . '</option>' . "\n" ;
                        }

                    }
                    
    $this -> _s_form_elements .=  '</select></td>' . "\n" ;

                }
                
    $this -> _s_form_elements .=  '</tr>' . "\n" ;
            }
        }
        public function 
    buildForm () {
            if(
    $this -> _checkTempBlock ()){
                
    $this -> _prepareForm ();
                
    $this -> _s_form .=  '<form action="' . htmlspecialchars ( $_SERVER [ 'PHP_SELF' ]). '" name="' . $this -> _s_formname . '" method="POST">' . "\n" ;
                
    $this -> _s_form .=  '<table name="' . $this -> _s_formname . '">' . "\n" ;
                
    $this -> _s_form .=  $this -> _s_form_elements ;
                
    $this -> _s_form .=  '</table>' . "\n" ;
                
    $this -> _s_form .=  '<input type="submit" name="' . $this -> s_submit_name . '" value="' . $this -> s_submit_value . '"/>' . "\n" ;
                
    $this -> _s_form .=  '</form>' . "\n" ;
                echo 
    $this -> _s_form ;
            }
            else {
                echo 
    'Es tut uns leid. Die IP-Adresse : \''  $_SERVER [ 'REMOTE_ADDR' ] . "' hat bereits eine Mail innerhalb <br />der letzten "
                
    . $this -> _i_reposttime  . " Sekunden getätigt.<br />" ;
                
    $sec  $_SESSION [ 'smoki_contact_form' ]+  $this -> _i_reposttime  time ();
                echo 
    'Warten Sie bitte noch '  $sec  . " Sekunden." ;
            }
            echo 
    var_dump ( $_SESSION [ 'smoki_contact_form' ]);

        }
        private function 
    _checkInputs () {
            foreach (
    $this -> _a_values  as  $key  =>  $value ){
                if(!
    $this -> _checkType ( $_POST [ $value [ 1 ]], $value [ 2 ], $value [ 3 ])) {
                    
    $this -> error .=  'Das Feld "' . $value [ 0 ]. '" enthält Fehler oder ist nicht vollständig ausgefüllt.'  . "<br />\n" ;
                }
            }
            if(
    $this -> error  ==  "" ) {
                
    $this -> _sendMail ();
            }
            else {
                echo 
    $this -> error ;
            }
        }
        public function 
    checkUserData () {
            if(
    $_POST [ $this -> s_submit_name ] ==  $this -> s_submit_value ) {
                
    $this -> _checkInputs ();
            }
        }
        private function 
    _prepareMessage () {
            
    $message .=  "Hallo \neine neue Nachricht vom " . $this -> _s_websitename . " Kontakformular.\n" ;
            
    $message .=  "Der Benutzer mit der IP: "  $_SERVER [ 'REMOTE_ADDR' ] . " schickte folgende Nachricht:\n\n" ;
            foreach (
    $this -> _a_values  as  $key  =>  $value ) {
                if(
    $_POST [ $value [ 1 ]] !=  ""  &&  $value [ 2 ] !=  'select'  ) {
                    
    $message .=  $value [ 0 ] . "\t\t "  . $_POST [ $value [ 1 ]]. "\n" ;
                }
                if(
    $_POST [ $value [ 1 ]] !=  ""  &&  $value [ 2 ] ==  'select'  ) {
                    
    $optn  $_POST [ $value [ 1 ]] - 1 ;
                    
    $message .=  $value [ 0 ] . "\t\t "  . $value [ 0 ][ $optn ]. "\n" ;
                }
            }
            
    $message .=  "\nAuf bald.....\nDein Mailformular." ;
            return 
    $message ;
        }
        private function 
    _getSubject () {
            foreach (
    $this -> _a_values  as  $key  =>  $value ){
                if(
    $value [ 1 ] ==  'subject' ) {
                    
    $subject  $value [ 0 ];
                }
            }
            if(
    $subject  ==  "" ) {
                
    $subject  'Kein Betreff' ;
            }
            return 
    $subject ;
        }
        private function 
    _checkTempBlock (){
            
    $akttime  time ();
            
    $block  $akttime  $_SESSION [ 'smoki_contact_form' ];
            if(
    $block  >=  $this -> _i_reposttime ){
                return 
    true ;
            }
            else {
                return 
    false ;
            }
        }
        private function 
    _blockTemporary () {
            
    $_SESSION [ 'smoki_contact_form' ] =  time ();

        }
        private function 
    _sendMail () {
            if(
    $this -> _checkTempBlock ()) {
                
    $subject  $this -> _getSubject ();
                
    $message  $this -> _prepareMessage ();

                
    #echo "Message $message <br>";

                
    $this -> _blockTemporary ();
                
    #$proof = @mail($this->_s_recipient,$subject,$message,"FROM: info@".$this->_s_websitename");
                
    if( $proof ) {
                    
    $this -> _blockTemporary ();
                }
                else {
                    
    "Aufgrund eines technischen Fehlers bitten wir Sie die Eingabe später erneut zu tätigen.\n" ;
                }
            }
        }
    }


    ?>
    Ok mal versucht so umzusetzen :D

    und zu meinem kleinen Problem noch ne idee?
     
  5. #4 22. März 2009
    AW: Mailform Klasse mit generiertem Inhalt

    zB sowas:
    PHP:
    <? php
    $form
    -> addFormElement (array(
      
    'mein Select'  => array(
            
    'optgroup #1'  => array(
                   
    'ohai'
                    
    'was'
            
    ),
             
    'Allgemein' , 'Technisch' , 'Privat'
      
    ),
      
    'subject'
      
    'select' );
    ?>
    $title => array(
    $optgrp_title => array(
    // alle options in der optgroup
    ),
    $option2,
    $option3, [...]
    );

    auswerten wäre sowas:
    PHP:
    <? php
     
    foreach( $select  as  $title  =>  $data ) {
       print 
    'Select: ' . $title . '<br />' ;
       print 
    '<select name="' . $title . '">' ;
       if(
    is_array ( $data )) {
         
    // multiple options?
        
    foreach( $data  as  $optTitle  =>  $option ) {
          if(
    is_array ( $option )) {
           
    // optgroup
          
    print  '<optgroup label="' . $optTitle . '">' ;
           foreach(
    $option  as  $op ) {
            print 
    '<option value="' . $op . '">' . $op . '</option>' ;
           }
          print 
    '</optgroup>' ;
          } else {
          
    // single option.
          
    print  '<option value="' . $option . '">' . $option . '</option>' ;
         }
        }
       } else {
        
    // only one option passed as string.
         
    print  '<option value="' . $data . '">' . $data . '</option>' ;
        }
      print 
    '</select>' ;
     }
    ?>

    lol. ka obs so direkt funktioniert, habs mal so "raw" hier hin geschrieben. hoffe du verstehst wie ich das mein :p
     
  6. #5 22. März 2009
    AW: Mailform Klasse mit generiertem Inhalt

    mhh joar, aber ich glaub das hätte meine ganze struktur neu umstrukturiert..

    ich hab einfach für arrays noch nen weiteren parameter inder addFormElement function gemacht.
    Is zwar nich die schickste variante, aber sonst müsste ich glaube ich nochmal alles umschreiben..
    aber ich schau mal ob ich die nochmal überarbeite und pimpe ...

    leider is das we zuende sonst würd ich mich heut nochma dran setzen, ich lass ma offen ich schreib die next we nochma um =) ..denk ich ^^

    achja gibts da noch ne andere möglichkeit außer ne tabelle für die beschriftung?
     
  7. #6 22. März 2009
    AW: Mailform Klasse mit generiertem Inhalt

    ich hab mir mal die regechsen angeschaut und verbessert:

    1. mail:
    Code:
    /^(?i)[a-z0-9\-\.\x7f-\xff]+@[a-z0-9\-\.\x7f-\xff]+\.[a-z\x7f-\xff]{2,}$/
    2. name
    --- da wärs sinnvoller wenn du zeichen angibst die NICHT enthalten sein sollen

    3. website:
    Code:
    /^(?i)https?:\/\/(?:[a-z0-9_\x7f-\xff]+\.)+[a-z\x7f-\xff]{2,}$/
    alternativ mit resource/query-string
    Code:
    /^(?i)https?:\/\/(?:[a-z0-9_\x7f-\xff]+\.)+[a-z\x7f-\xff]{2,}(?:\/[a-z\/\?_\-\+%0-9\.]+)?$/
    die teile sollten nur mit preg_* funktionieren
     
  8. #7 22. März 2009
    AW: Mailform Klasse mit generiertem Inhalt

    also mal abgesehen das mir die statements schon wieder ein wenig zu über gehen ;D
    ( kenns tmich ja , ich und regex's )

    hab ich das aber mal eingesetzt....

    sieht nu folgendermaßen aus :
    PHP:
    <? php


    class  MailForm  {
        private 
    $_s_recipient ;
        private 
    $_s_websitename ;
        private 
    $_s_formname  'mail_form' ;
        private 
    $_a_values  = array();
        private 
    $_s_form_elements ;
        private 
    $_s_form ;
        private 
    $_i_reposttime  600 ;
        public  
    $s_submit_value  'Absenden' ;
        public  
    $s_submit_name  'sendform' ;
        public  
    $error ;

        function 
    __construct ( $s_email , $_s_websitename ) {
            
    session_start ();
            if(
    $this -> _checkType ( $s_email , 'email' , 1 )) {
                
    $this -> _s_recipient  $s_email ;
                return 
    true ;
            }
            else {
                echo 
    'No valid email is given....' ;
                return 
    false ;
            }
            
    $this -> _s_websitename  $_s_websitename ;

        }
        public function 
    setReposteTime ( $time ) {
            
    $this -> _i_reposttime  $time ;
        }
        public function 
    addFormElement ( $s_description , $s_name , $s_type , $i_need  1 , $s_seldesc = "" ) {
            
    $this -> _a_values [] = array ( $s_description , $s_name , $s_type , $i_need , $s_seldesc );
        }
        public function 
    buildForm () {
            if(
    $this -> _checkTempBlock ()){
                
    $this -> _prepareForm ();
                
    $this -> _s_form .=  '<form action="kontakt.html" name="' . $this -> _s_formname . '" method="POST">' . "\n" ;
                
    $this -> _s_form .=  '<table name="' . $this -> _s_formname . '">' . "\n" ;
                
    $this -> _s_form .=  $this -> _s_form_elements ;
                
    $this -> _s_form .=  '</table>' . "\n" ;
                
    $this -> _s_form .=  '<input type="submit" name="' . $this -> s_submit_name . '" value="' . $this -> s_submit_value . '"/>' . "\n" ;
                
    $this -> _s_form .=  '</form>' . "\n" ;
                echo 
    $this -> _s_form ;
            }
            else {
                echo 
    'Es tut uns leid. Die IP-Adresse : \''  $_SERVER [ 'REMOTE_ADDR' ] . "' hat bereits eine Mail innerhalb <br />der letzten "
                
    . $this -> _i_reposttime  . " Sekunden getätigt.<br />" ;
                
    $sec  $_SESSION [ 'smoki_contact_form' ]+  $this -> _i_reposttime  time ();
                echo 
    'Warten Sie bitte noch '  $sec  . " Sekunden." ;
            }
            
    #echo var_dump($_SESSION['smoki_contact_form']);

        
    }
        public function 
    checkUserData () {
            if(
    $_POST [ $this -> s_submit_name ] ==  $this -> s_submit_value ) {
                
    $this -> _checkInputs ();
            }
        }
        private function 
    _checkType ( $s_val , $s_type , $i_need ) {
            
    #echo "testing : $s_val for $s_type required: $i_need <br>";
            
    switch ( $s_type ) {
                case 
    'email'         : if( $i_need  ==  &&  $s_val  ==  "" ) { return  true ; } else { if(( preg_match ( "/^(?i)[a-z0-9\-\.\x7f-\xff]+@[a-z0-9\-\.\x7f-\xff]+\.[a-z\x7f-\xff]{2,}$/" , $s_val ) &&  $s_val  !=  ""  &&  strlen ( $s_val ) >=  &&  strlen ( $s_val ) <=  30 )) { return  true ; } else { return  false ; } ;} ; break;
                case 
    'number'        : if( $i_need  ==  &&  $s_val  ==  "" ) { return  true ; } else { if( strlen ((int) $s_val ) >  ) { return  true ; } else { return  false ; } ;} ; break;
                case 
    'name'          : if( $i_need  ==  &&  $s_val  ==  "" ) { return  true ; } else { if( preg_match ( "/^[a-zäüö][a-zäüöß\s_-]+[a-zöüß]/i" , $s_val ) &&  strlen ( $s_val ) >  6 ) { return  true ; } else { return  false ; } ;} ; break;
                case 
    'textarea'      : if( $i_need  ==  &&  $s_val  ==  "" ) { return  true ; } else {  $s_val  strip_tags ( $s_val ); if( strlen ( $s_val ) >=  30  &&  strlen ( $s_val ) <  600  ) { return  true ; } else { return  false ; } ;} ; break;
                case 
    'select'        : if( $i_need  ==  &&  $s_val  ==  "" ) { return  true ; } else { if((int) $s_val  !=  "" ) { return  true ; } else { return  false ; } ;} ; break;
                case 
    'website'       : if( $i_need  ==  &&  $s_val  ==  "" ) { return  true ; } else { if( preg_match ( "/^(?i)https?:\/\/(?:[a-z0-9_\x7f-\xff]+\.)+[a-z\x7f-\xff]{2,}$/" , $s_val )) { return  true ; } else { return  false ; } ;} ; break;
                default             : return 
    false ; break;
            }
        }
        private function 
    _prepareForm () {
            foreach (
    $this -> _a_values  as  $arg  =>  $val ){
                
    $this -> _s_form_elements .=  '<tr>' . "\n" ;
                if(
    $val [ 2 ] !=  "select"  &&  $val [ 2 ] !=  "textarea" ) {
                    
    $this -> _s_form_elements .=  '<td>' . $val [ 0 ];
                    if(
    $val [ 3 ] ==  1 ) {
                        
    $this -> _s_form_elements .=  '*' ;
                    }
                    
    $this -> _s_form_elements .=  '</td><td><input type="text" name="' . $val [ 1 ]. '" value="' . $_POST [ $val [ 1 ]]. '" /></td>' . "\n" ;
                }
                if(
    $val [ 2 ] ==  "textarea" ){
                    
    $this -> _s_form_elements .=  '<td>' $val [ 0 ];
                    if(
    $val [ 3 ] ==  1 ) {
                        
    $this -> _s_form_elements .=  '*' ;
                    }
                    
    $this -> _s_form_elements .=  '</td>' . "\n" ;
                    
    $this -> _s_form_elements .=  '<td><textarea name="' . $val [ 1 ] . '">' . $_POST [ $val [ 1 ]]. '</textarea></td>' . "\n" ;
                }
                if(
    $val [ 2 ] ==  'select' ) {
                    
    $this -> _s_form_elements .=  '<td>' . $val [ 4 ];
                    if(
    $val [ 3 ] ==  1 ) {
                        
    $this -> _s_form_elements .=  '*' ;
                    }
                    
    $this -> _s_form_elements .=  '</td><td><select name="' . $val [ 1 ]. '">' . "\n" ;
                    
    $this -> _s_form_elements .=  '<option value=""></option>' . "\n" ;

                    
    $frm_name  $val [ 1 ];
                    foreach (
    $val [ 0 ] as  $opt  =>  $desc ) {
                        
    $optn  $opt + 1 ;
                        if(
    $_POST [ $frm_name ] == (int)  $optn ) {
                            
    $this -> _s_form_elements .=  '<option selected="selected" value="' . $optn . '">' . $desc . '</option>' . "\n" ;
                        }else {
                            
    $this -> _s_form_elements .=  '<option value="' . $optn . '">' . $desc . '</option>' . "\n" ;
                        }

                    }
                    
    $this -> _s_form_elements .=  '</select></td>' . "\n" ;

                }
                
    $this -> _s_form_elements .=  '</tr>' . "\n" ;
            }
        }
        private function 
    _checkInputs () {
            foreach (
    $this -> _a_values  as  $key  =>  $value ){
                if(!
    $this -> _checkType ( $_POST [ $value [ 1 ]], $value [ 2 ], $value [ 3 ])) {
                    
    $this -> error .=  'Das Feld "' . $value [ 0 ]. '" enthält Fehler oder ist nicht vollständig ausgefüllt.'  . "<br />\n" ;
                }
            }
            if(
    $this -> error  ==  "" ) {
                
    $this -> _sendMail ();
            }
            else {
                echo 
    '<div class="' . $this -> s_formname . '_error">' . "\n" ;
                echo 
    $this -> error ;
                echo 
    '</div>' . "\n" ;
            }
        }
        private function 
    _prepareMessage () {
            
    $message .=  "Hallo \neine neue Nachricht vom " . $this -> _s_websitename . " Kontakformular.\n" ;
            
    $message .=  "Der Benutzer mit der IP: "  $_SERVER [ 'REMOTE_ADDR' ] . " schickte folgende Nachricht:\n\n" ;
            foreach (
    $this -> _a_values  as  $key  =>  $value ) {
                if(
    $_POST [ $value [ 1 ]] !=  ""  &&  $value [ 2 ] !=  'select'  ) {
                    
    $message .=  $value [ 0 ] . "\t\t "  . $_POST [ $value [ 1 ]]. "\n" ;
                }
                if(
    $_POST [ $value [ 1 ]] !=  ""  &&  $value [ 2 ] ==  'select'  ) {
                    
    $optn  $_POST [ $value [ 1 ]] - 1 ;
                    
    $message .=  $value [ 0 ] . "\t\t "  . $value [ 0 ][ $optn ]. "\n" ;
                }
            }
            
    $message .=  "\nAuf bald.....\nDein Mailformular." ;
            return 
    $message ;
        }
        private function 
    _getSubject () {
            foreach (
    $this -> _a_values  as  $key  =>  $value ){
                if(
    $value [ 1 ] ==  'subject' ) {
                    
    $subject  $value [ 0 ];
                }
            }
            if(
    $subject  ==  "" ) {
                
    $subject  'Kein Betreff' ;
            }
            return 
    $subject ;
        }
        private function 
    _checkTempBlock (){
            
    $akttime  time ();
            
    $block  $akttime  $_SESSION [ 'smoki_contact_form' ];
            if(
    $block  >=  $this -> _i_reposttime ){
                return 
    true ;
            }
            else {
                return 
    false ;
            }
        }
        private function 
    _blockTemporary () {
            
    $_SESSION [ 'smoki_contact_form' ] =  time ();

        }
        private function 
    _sendMail () {
            if(
    $this -> _checkTempBlock ()) {
                
    $subject  $this -> _getSubject ();
                
    $message  $this -> _prepareMessage ();

                
    #echo "Message $message <br>";

                
    $this -> _blockTemporary ();
                
    #$proof = @mail($this->_s_recipient,$subject,$message,"FROM: info@".$this->_s_websitename");
                
    if( $proof ) {
                    
    $this -> _blockTemporary ();
                }
                else {
                    
    "Aufgrund eines technischen Fehlers bitten wir Sie die Eingabe später erneut zu tätigen.\n" ;
                }
            }
        }
    }


    ?>
    Auch wenn mir die lösung mit dem array ($s_description,$s_name,$s_type,$i_need,$s_seldesc); noch nit gefällt, aber die schau ich mir next week nochma an ....


    eine sache hab ich aber heut noch .. ich nutze mit meiner seite mod_rewrite....

    Ich generiere ja den Formularnamen eigentlich per $_SERVER['PHP_SELF'] dabei kommt dann aber index.php raus anstatt "URL.de/bereich/kontakt.html" kann ich das irgendwie umgehen?
     
  9. #8 22. März 2009
    AW: Mailform Klasse mit generiertem Inhalt

    $_SERVER['REQUEST_URI'];

    btw:

    anstelle von
    private $_var;
    private $_var2;
    private $_var3;

    -
    private $_var = 'o',
    $_var2 = 'oo',
    $_var3 = 'pq';
     
  10. #9 22. März 2009
    AW: Mailform Klasse mit generiertem Inhalt

    Da schon 2x der RegEx verbessert wurde wollte ich mal fragen ob es einen bestimmten Grund gibt nicht die PHP-Funktion filter_var() mit dem validate_email filter zu benutzen???

    PHP:
    <? php
    $email 
    "sag@ich.net" ;
    if(
    filter_var ( $email filter_id ( "validate_email" )) ===  false )
      
    // Some code
    ?>
     

  11. Videos zum Thema
Die Seite wird geladen...
Similar Threads - PHP Mailform Klasse
  1. Antworten:
    0
    Aufrufe:
    2.255
  2. Antworten:
    0
    Aufrufe:
    3.322
  3. Antworten:
    4
    Aufrufe:
    6.191
  4. Antworten:
    5
    Aufrufe:
    15.295
  5. Gute PHP Template Engine

    Yuesang , 20. Februar 2017 , im Forum: Webentwicklung
    Antworten:
    4
    Aufrufe:
    5.534
  • Annonce

  • Annonce