public static function save_uploaded_image(&$input)
 {
     $tmp_name = $_FILES['picture']['tmp_name'];
     $input['picture']->tmp_name = basename($tmp_name) . '.jpg';
     $main_image = Persil::create_resized_image($tmp_name, 'uploads/' . basename($tmp_name) . '.jpg', 510, 338, false, 'img/ui/frame_back.jpg');
     // resize full image to fit within maximum bounds of the design
     $thumbnail = Persil::create_resized_image($tmp_name, 'uploads/thumbs/' . basename($tmp_name) . '.jpg', 316, 210, false, 'img/ui/frame_back.jpg');
     // generate thumbnail
     $thanks_image = Persil::create_resized_image($tmp_name, 'uploads/thumbs/' . basename($tmp_name) . '-thanks.jpg', 510, 338, false, 'img/ui/frame_back.jpg');
     // generate thanks thumbnail
     return $main_image && $thumbnail && $thanks_image;
 }
 public function process_form()
 {
     if (!count($_POST)) {
         return View::make('includes/template')->with('template', 'form');
     } else {
         Validator::extend('day_of_month', function ($attribute, $value, $parameters) {
             $month = intval($_POST['dob_month']);
             $year = intval($_POST['dob_year']) ? intval($_POST['dob_year']) : 2013;
             if (!$month) {
                 return false;
             }
             $total_days = date("t", mktime(1, 1, 1, $month, 1, $year));
             return intval($value) <= $total_days;
         });
         $rules = array('picture' => 'Required|Mimes:jpeg|Max:2048', 'image_title' => array('Required', 'Regex:/[\\p{L}\\-_ 0-9]+/u', 'Max:150'), 'drawn_by' => array('Required', 'Regex:/[\\p{L}][\\p{L}\\- ]+/u', 'Max:150'), 'dob_year' => 'Required|Integer|Between:1999,2013', 'dob_month' => 'Required|Integer|Between:1,12', 'dob_day' => 'Required|Integer|Between:1,31|day_of_month', 'title' => array('Required', 'Regex:/^(Mr|Mrs|Ms|Miss|Other)$/'), 'forename' => array('Required', 'Regex:/[\\p{L}][\\p{L}\\- ]+/u', 'Max:150'), 'surname' => array('Required', 'Regex:/[\\p{L}][\\p{L}\\- ]+/u', 'Max:150'), 'address' => array('Required', 'Regex:/[\\p{L}\\-_ 0-9]+/u', 'Max:150'), 'town' => array('Required', 'Max:50', 'Regex:/[\\p{L}\\- ]+/u'), 'country' => 'In:England,Scotland,Wales,Northern Ireland,Republic of Ireland', 'postcode' => array('Regex:/^([a-pr-uwyz]((\\d{1,2})|([a-hk-y]\\d{1,2})|(\\d[a-hjkstuw])|([a-hk-y]\\d[a-z])) *\\d[abd-hjlnp-uw-z]{2})?$/i', 'Max:10'), 'email' => 'Required|Email|Confirmed', 'email_confirmation' => 'Required|Email', 'terms' => 'Required');
         $messages = array('picture.mimes' => "Sorry - our systems don't recognise the type of file you've uploaded. Please have another go with a jpg file", 'picture.max' => "Sorry - the file you've tried to upload is too big for our systems! Please have another go with a smaller jpg", 'image_title.required' => "Oops, your drawing doesn't have a title", 'drawn_by.required' => "Sorry, our systems don't recognise the drawer’s name you've entered. We just want a first name with no numbers or symbols and it must start with a letter. Please try again!", 'drawn_by.regex' => 'Sorry, the name can only contain letters, spaces, and hyphens and it must start with a letter', 'dob_year:required' => 'Sorry, you must enter a year of birth', 'dob_year.integer' => 'Sorry, your year of birth must be between 1999 and the present', 'dob_year.between' => 'Sorry, your year of birth must be between 1999 and the present', 'dob_month:required' => 'Sorry, you must enter the month the drawer was born', 'dob_month.integer' => 'Sorry, the month the drawer was born must be a number from 1 to 12', 'dob_month.between' => 'Sorry, the month the drawer was born must be a number from 1 to 12', 'dob_day.required' => 'Sorry, you need to enter the day of the month the drawer was born', 'dob_day.integer' => 'Sorry, the day of the month the drawer was born should be between 1 and 31', 'dob_day.between' => 'Sorry, the day of the month the drawer was born should be between 1 and 31', 'dob_day.day_of_month' => 'Sorry, that month doesn\'t have that many days!', 'forename.required' => "Hello stranger. We're missing your first name.", 'forename.regex' => 'Sorry, the name can only contain letters, spaces, and hyphens', 'surname.required' => "We're missing your surname.", 'surname.regex' => 'Sorry, the name can only contain letters, spaces, and hyphens', 'address.required' => "We need your address so, if you win, the postman knows where to deliver your petrifying prize.", 'town.required' => "Where do you live?", 'postcode.required' => "Don't forget to pop in your post code.", 'postcode.regex' => "Sorry, our systems don’t recognise what you've entered as a post code. Please use a regular UK format, like KT22 7GR.", 'email.required' => "What's your email?", 'email.email' => "Sorry, our systems don't recognise what you've entered as an email. Please use a regular email format, like info@Persil.com", 'email.confirmed' => "Sorry, the emails you’ve entered don’t match up. Please check them and have another go.", 'terms.required' => "See that tiny box? You need to tick it if you agree to the terms and conditions.");
         $input = Input::all();
         // note to self - all() is required instead of get() if processing uploads - just wasted a good hour hunting down this little bug
         $input['dob_day'] = trim($input['dob_day'], '0');
         $input['dob_month'] = trim($input['dob_month'], '0');
         $validator = Validator::make($input, $rules, $messages);
         if ($validator->fails()) {
             // spit out the form again with the errors shown
             return View::make('includes/template')->with('template', 'form')->with('errors', $validator->messages())->with('input', $input);
         } else {
             // first - process the image
             $image = Persil::save_uploaded_image($input);
             $added = Persil::insert_user_data($input);
             $traction_added = Persil::post_traction_data($input, $this->url, $this->password);
             if ($image && $added) {
                 // push the validated data into the session so that it can be shown in the form again if they go to make another submission
                 Session::put('submitted_data', array());
                 foreach ($input as $key => $value) {
                     if ($key != 'picture') {
                         Session::push("submitted_data.{$key}", $value);
                     }
                 }
                 //Session::put('submitted_data', $input);
                 return View::make('includes/template')->with('template', 'confirmation')->with('input', $input);
             } else {
                 echo 'error page';
             }
             // this will probably be the form again with some generic error message or something
         }
     }
 }