- <?php
- /**
- * Ajax Newsletter signup
- * checking email and stance for razorboarders.
- **/
- // Includes first
- require_once 'is_email/is_email.php'; // library for email validation
- // Initialize constants and variables
- define( 'SUBSCRIBERS', 'db/subscribers.xml' );
- /* xml file must have a tag present:
- <?xml version="1.0"?>
- <subscribers></subscribers>
- */
- $email = $_REQUEST['email']; // Todo: validate input
- $email = "no@no.no";
- $stance = $_REQUEST['stance'];
- $stance = "switch";
- // Check email not empty and valid (is_email often fails on dns checks, add "false" to disable)
- if( !empty( $email ) && is_email( $email ) )
- {
- // Check xml file exists and writable
- if( file_exists( SUBSCRIBERS ) && is_writable( SUBSCRIBERS ) )
- {
- // Create simplexml object
- $xml = simplexml_load_file( SUBSCRIBERS );
- $sxml = new SimpleXMLElement( $xml->asXML() ); // Can get variable from php file here and skip last line.
- // Loop through subscribers
- foreach( $sxml->person as $person )
- {
- // Die if email exists
- if( (string) $person->email === $email )
- die( "Email already in list, you can subscribe again with a different one if you wish." );
- }
- // Create new person xml tag
- $new_person = $sxml->addChild( 'person' );
- // Add email
- $new_person->addChild( 'email', $email );
- // Add stance
- switch( (string) $stance )
- {
- case 'regular':
- $new_person->addChild( 'stance', 'regular' ); // coule be $stance, but not sure if always lowercase then
- break;
- case 'goofy':
- $new_person->addChild( 'stance', 'goofy' );
- break;
- case 'switch':
- $new_person->addChild( 'stance', 'switch' );
- break;
- }
- //$rating->addAttribute('type', 'racing'); // off-piste, pipe, freestyle, bigjump, kids..
- // Write to xml file
- $sxml->asXML( SUBSCRIBERS );
- echo "Ok\n"; //or true etc..
- }
- else
- die( "File error." );
- }
- else
- die( "Email not valid, please try again." );