<?php
        if(isset($_FILES) && (bool) $_FILES)
        {

            // Assign Variables
            $form_name = filter_var($_POST['form_name'], FILTER_SANITIZE_STRING);
            $form_prenom = filter_var($_POST['form_prenom'], FILTER_SANITIZE_STRING);
            $form_email = filter_var($_POST['form_email'], FILTER_SANITIZE_EMAIL);
            $form_adresse = filter_var($_POST['form_adresse'], FILTER_SANITIZE_STRING);
            $form_ville = filter_var($_POST['form_ville'], FILTER_SANITIZE_STRING);
            $form_metier = filter_var($_POST['form_metier'], FILTER_SANITIZE_STRING);
            // Creating Array of Errors
            $formErrors = array();
            if (strlen($form_name) <= 3) {
                $formErrors[] = 'Le Nom d\'utilisateur doit être plus grand que <strong>3</strong> caractères';
            }
            if (strlen($form_prenom) <= 3) {
                $formErrors[] = 'Le Prenom d\'utilisateur doit être plus grand que <strong>3</strong> caractères';
            }
            if (strlen($form_ville) <= 3) {
                $formErrors[] = 'Ville ne peut pas être inférieur à <strong>10</strong> caractères';
            }
            if (strlen($form_metier) <= 10) {
                $formErrors[] = 'Metier ne peut pas être inférieur à <strong>10</strong> caractères';
            }
            if (strlen($form_adresse) <= 10) {
                $formErrors[] = 'Le Adresse ne peut pas être inférieur à <strong>10</strong> caractères';
            }

            $allowedExtensions = array("pdf","doc","docx","gif","jpeg","jpg","png","rtf","txt","pptx");
           $files = array();
        	foreach($_FILES as $name=>$file) {
        		$file_name = $file['name'];
        		$temp_name = $file['tmp_name'];
        		$file_type = $file['type'];
        		$path_parts = pathinfo($file_name);
        		$ext = $path_parts['extension'];
        		if(!in_array($ext,$allowedExtensions)) {
        			die("Fichier $file_name a les extensions $ext non autorisées");

        		}
        		array_push($files,$file);
        	}

        	// email fields: to, from, subject, and so on
        	$to = "contact@galaxysolutions.ma";
        	$from = 'contact@galaxysolutions.ma';
        	$subject ="Rejoignez-Nous";

            $msg="Bonjour"." ". $form_name . "\n\n" ."Nous vous remercions pour votre interét et votre Confiance." . "\n\n"
                . "Votre Inscription sur Galaxy Solutions a bien été enregistrée" ."\n\n"
                ."Nous vous rappelons vos identifiants de connexion utilisateur :" .$form_email ."\n\n\n\n\n\n"."Cordialement."."\n\n"."l'equipe GALAXY SOLUTIONS";
            $email="contact@galaxysolutions.ma";
        	$message = "Les informtion Utilisateur "   . "\n".
        	           "Nom     est: "  .$form_name    . "\n".
        	           "Prenom  est: "  .$form_prenom  . "\n".
        	           "Adresse est: "  .$form_adresse . "\n".
        	           "Email   est: "  .$form_email   . "\n".
        	           "Ville   est: "  .$form_ville   . "\n".
        	           "Metier  est: "  .$form_metier;
        	 $headers = "From: $from\r\nReply-to: $form_email";

        	//boundary
        	$semi_rand = md5(time());
        	$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";

        	// headers for attachment
        	$headers .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"{$mime_boundary}\"";

        	// multipart boundary
        	$message = "This is a multi-part message in MIME format.\n\n" . "--{$mime_boundary}\n" . "Content-Type: text/plain; charset=\"iso-8859-1\"\n" . "Content-Transfer-Encoding: 7bit\n\n" . $message . "\n\n";
        	$message .= "--{$mime_boundary}\n";


            // preparing attachments
            	for($x=0;$x<count($files);$x++){
            		$file = fopen($files[$x]['tmp_name'],"rb");
            		$data = fread($file,filesize($files[$x]['tmp_name']));
            		fclose($file);
            		$data = chunk_split(base64_encode($data));
            		$name = $files[$x]['name'];
            		$message .= "Content-Type: {\"application/octet-stream\"};\n" . " name=\"$name\"\n" .
            		"Content-Disposition: attachment;\n" . " filename=\"$name\"\n" .
            		"Content-Transfer-Encoding: base64\n\n" . $data . "\n\n";
            		$message .= "--{$mime_boundary}\n";
            	}

                if (empty($formErrors)) {

                    mail($to, $subject, $message, $headers);
                        mail($form_email,$subject,$msg, 'From: '.$email );
                            $form_name      ='';
                            $form_prenom    ='';
                            $form_email     ='';
                            $form_adresse   ='';
                            $form_ville     ='';
                            $form_metier    ='';
                             $success = '
                            <div class="alert alert-success alert-dismissible" role="start">
                                <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                                    <span aria-hidden="true">&times;</span>
                                </button>
                                vos informations ont été envoyées avec succès
                            </div>';
                    }
        }
?>
<?php
require_once 'navbar/header.php';
?>

<div class="inner-banner text-center">
    <div class="container">
        <div class="box">
            <h3>Rejoignez-nous !</h3>
        </div><!-- /.box -->
    </div><!-- /.container -->
</div>

<section class="Rejoignez  sec-padd">
    <div class="container">
        <div class="row">
            <div class="col-md-8 col-md-offset-2 col-sm-12 col-xs-12">
            <div class="default-form-area">
                    <form id="mailForm" name="mailForm" class="default-form" action="" method="post" enctype="multipart/form-data" autocomplete="off">
                            <?php if (! empty($formErrors)) { ?>
                        <div class="alert alert-danger alert-dismissible" role="start">
                            <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                                <span aria-hidden="true">&times;</span>
                            </button>
                            <?php
                                foreach($formErrors as $error) {
                                    echo $error . '<br/>';
                                }
                            ?>
                        </div>
                        <?php } ?>
                        <?php if (isset($success)) { echo $success; } ?>
                        <div class="row clearfix">
                            <div class="col-md-6 col-sm-6 col-xs-12">
                                <div class="form-group">
                                    <label>NOM *</label>
                                    <input type="text" name="form_name" class="form-control" value=""  required="">
                                </div>
                            </div>
                            <div class="col-md-6 col-sm-6 col-xs-12">
                                <div class="form-group">
                                    <label>PRENOM *</label>
                                    <input type="text" name="form_prenom" class="form-control" value="" required="">
                                </div>
                            </div>
                            <div class="col-md-6 col-sm-6 col-xs-12">
                                <div class="form-group">
                                    <label>Email *</label>
                                    <input type="email" name="form_email" class="form-control required email" value=""  required="">
                                </div>
                            </div>
                            <div class="col-md-6 col-sm-6 col-xs-12">
                                <div class="form-group">
                                    <label>ADRESSE *</label>
                                    <input type="text" name="form_adresse" class="form-control" value="" required="">
                                </div>
                            </div>
                            <div class="col-md-6 col-sm-6 col-xs-12">
                                <div class="form-group">
                                    <label>VILLE  *</label>
                                    <input type="text" name="form_ville" class="form-control" value=""  required="">
                                </div>
                            </div>
                            <div class="col-md-6 col-sm-6 col-xs-12">
                                <div class="form-group">
                                    <label>METIER *</label>
                                    <input type="text" name="form_metier" class="form-control" value=""  required="">
                                </div>
                            </div>
                            <div class="col-md-6 col-sm-6 col-xs-12">
                                <div class="form-group">
                                    <label>CV *</label>
                                    <input type="file" name="form_cv" class="form-control" required="">
                                </div>
                            </div>
                            <div class="col-md-6 col-sm-6 col-xs-12">
                                <div class="form-group">
                                    <label>LETTRE DE MOTIVATION *</label>
                                    <input type="file" name="form_lettre_motivation" class="form-control"  required="">
                                </div>
                            </div>
                            <div class="col-md-12 col-sm-12 col-xs-12">
                                <div class="form-group">
                                    <input id="form_botcheck" name="form_botcheck" class="form-control" type="hidden" value="">
                                    <button class="thm-btn2" type="submit">VALIDER</button>
                                </div>
                            </div>

                        </div>
                    </form>
                </div>
            </div>
        </div>

    </div>
</section>


<?php
require_once 'navbar/footer.php'
?>
