Contact_PHP

This is the main PHP script that controls the contacts page

    1| <?php
    2|     //Bring in access to master head and submaster head
    3|     require("../showcase/scripts/showcase_rtns.php");
    4| 
    5|     //Bring in contact form page
    6|     require("contact_form.php");
    7| 
    8|     //Bring in mail form page
    9|     require("contact_mail.php");
  10| 
  11|     //Bring in captcha and validation
  12|     require("captcha.php");
  13| 
  14|     //Bring in mysql token routines
  15|     require("contact_mysql_rtns.php");
  16| 
  17|     //Bring in custom errors
  18|     require("contact_error_rtns.php");
  19| 
  20|     //Database general info
  21|     $host = "[HOST NAME GOES HERE]";
  22|     $user = "[USER NAME GOES HERE]";
  23|     $pass = "[PASSWORD GOES HERE]";
  24|     $DBName = "[DATABASE NAME GOES HERE]";
  25|     $table = "[DATABASE TABLE NAME GOES HERE]";
  26| 
  27|     //token variable
  28|     $hTok = "";
  29| 
  30|     //developer mode
  31|     $dev = true;
  32| 
  33| ?>
  34| 
  35| <?php
  36|     /*
  37|         Use $_SERVER['REQUEST_METHOD'] to consolidate form front end webpage and back end server script as recommended in Beginning PHP and MySQL: From Novice to Professional (2nd Edition) 9.1 pp.261-262
  38|         (http://www.amazon.com/gp/product/1590598628?ie=UTF8&tag=keithgallcom-20&linkCode=as2&camp=1789&creative=390957&creativeASIN=1590598628).
  39|     */
  40|     if($_SERVER['REQUEST_METHOD'] =='GET')
  41|     {
  42|         /*
  43|         Never hard code any variable as I was taught at Northcentral Technical College.
  44|         Use $_SERVER['SCRIPT_NAME'] so that the form's action variable will always point to PHP script no matter where this script is on the server as recommended in Beginning PHP and MySQL: From Novice to Professional (2nd Edition) 9.1 pp.261-262
  45|         (http://www.amazon.com/gp/product/1590598628?ie=UTF8&tag=keithgallcom-20&linkCode=as2&camp=1789&creative=390957&creativeASIN=1590598628).
  46|         */
  47|         $ScriptName = $_SERVER['SCRIPT_NAME'];
  48|         $hTok = createHash($DBName, $host, $user, $pass, $table, $dev);
  49|         $CapA = CapRandNum(1,10);
  50|         $CapB = CapRandNum(1,10);
  51|         $CapSign = CapRandSign();
  52|         contactPage('Contact', $ScriptName, '', '', '', '', '', '', null, $hTok, $CapA, $CapB, $CapSign, '');
  53|     }
  54|     else
  55|     {
  56|         $serverScript = $_POST['script'];
  57|         $firstName = $_POST['firstName'];
  58|         $lastName = $_POST['lastName'];
  59|         $email = $_POST['email'];
  60|         $subject = $_POST['subject'];
  61|         $message = $_POST['message'];
  62|         $hTok = $_POST['token'];
  63|         $capAnswer = $_POST['capAnswer'];
  64|         $capComp = $_POST['capComp'];
  65| 
  66|         /*use unique button names for each 'submit' button in form like Parrots over at (http://stackoverflow.com/questions/547821/two-submit-buttons-in-one-form) recommends in his answer.*/
  67|         if($_POST['submit'])
  68|         {
  69|             $sLen = strlen($capComp);
  70| 
  71|             $CommaOne = stripos($capComp,",");
  72|             $CommaTwo = stripos($capComp,",",$CommaOne+1);
  73| 
  74|             $lenTwo = ($CommaTwo - $CommaOne)-1;
  75|             $lenThree = ($sLen - $CommaTwo)-1;
  76| 
  77|             $CapA = substr($capComp,0,$CommaOne);
  78|             $CapSign = substr($capComp,$CommaOne+1,$lenTwo);
  79|             $CapB = substr($capComp,$CommaTwo+1,$lenThree);
  80| 
  81|             $Switch = true;
  82| 
  83|             $ErrorArray = array();
  84| 
  85|             $numSw = checkHash($DBName, $host, $user, $pass, $table, $hTok, $firstName, $lastName, $email, $subject, $message, $serverScript, $CapA, $CapB, $CapSign, $capAnswer, $dev);
  86| 
  87| 
  88|             if($numSw == 1)
  89|             {
  90|                 $pError = "<p>Our records show your form has already been sent and processed.</p>";
  91|                 contactPage('Contact Error(s)', $serverScript, $firstName, $lastName, $email, $subject, $message, $pError, null, $hTok, $CapA, $CapB, $CapSign, $capAnswer);
  92| 
  93|             }
  94| 
  95|             if($numSw == 0)
  96|             {
  97|                 if(!validateNotEmpty($firstName))
  98|                 {
  99|                     $ErrorArray[0] = "visible";
100|                     $ErrorArray[1] = "inline";
101|                     $Switch = false;
102|                 }
103|                 else
104|                 {
105|                     $ErrorArray[0] = "none";
106|                     $ErrorArray[1] = "hidden";
107|                 }
108| 
109|                 if(!validateNotEmpty($lastName))
110|                 {
111|                     $ErrorArray[2] = "visible";
112|                     $ErrorArray[3] = "inline";
113|                     $Switch = false;
114|                 }
115|                 else
116|                 {
117|                     $ErrorArray[2] = "none";
118|                     $ErrorArray[3] = "hidden";
119|                 }
120| 
121|                 if(!validateNotEmpty($email))
122|                 {
123|                     $ErrorArray[4] = "visible";
124|                     $ErrorArray[5] = "inline";
125|                     $ErrorArray[6] = "none";
126|                     $ErrorArray[7] = "hidden";
127|                     $Switch = false;
128|                 }
129|                 elseif(!validateEmail($email))
130|                 {
131|                     $ErrorArray[4] = "none";
132|                     $ErrorArray[5] = "hidden";
133|                     $ErrorArray[6] = "visible";
134|                     $ErrorArray[7] = "inline";
135|                     $Switch = false;
136|                 }
137|                 else
138|                 {
139|                     $ErrorArray[4] = "none";
140|                     $ErrorArray[5] = "hidden";
141|                     $ErrorArray[6] = "none";
142|                     $ErrorArray[7] = "hidden";
143|                 }
144| 
145|                 if(!validateSubject($subject))
146|                 {
147|                     $ErrorArray[8] = "visible";
148|                     $ErrorArray[9] = "inline";
149|                     $Switch = false;
150|                 }
151|                 else
152|                 {
153|                     $ErrorArray[8] = "none";
154|                     $ErrorArray[9] = "hidden";
155|                 }
156| 
157|                 if(!validateNotEmpty($message))
158|                 {
159|                     $ErrorArray[10] = "visible";
160|                     $ErrorArray[11] = "inline";
161|                     $Switch = false;
162|                 }
163|                 else
164|                 {
165|                     $ErrorArray[10] = "none";
166|                     $ErrorArray[11] = "hidden";
167|                 }
168| 
169|                 if(!validateCapEmpty($capAnswer))
170|                 {
171|                     $ErrorArray[12] = "visible";
172|                     $ErrorArray[13] = "inline";
173|                     $ErrorArray[14] = "none";
174|                     $ErrorArray[15] = "hidden";
175|                     $ErrorArray[16] = "none";
176|                     $ErrorArray[17] = "hidden";
177|                     $Switch = false;
178|                 }
179|                 else if(!validateCapNum($capAnswer))
180|                 {
181|                     $ErrorArray[12] = "none";
182|                     $ErrorArray[13] = "hidden";
183|                     $ErrorArray[14] = "visible";
184|                     $ErrorArray[15] = "inline";
185|                     $ErrorArray[16] = "none";
186|                     $ErrorArray[17] = "hidden";
187|                     $Switch = false;
188|                 }
189|                 else if(!validateCapCorrect($capAnswer,$CapA,$CapB,$CapSign))
190|                 {
191|                     $ErrorArray[12] = "none";
192|                     $ErrorArray[13] = "hidden";
193|                     $ErrorArray[14] = "none";
194|                     $ErrorArray[15] = "hidden";
195|                     $ErrorArray[16] = "visible";
196|                     $ErrorArray[17] = "inline";
197|                     $Switch = false;
198|                 }
199|                 else
200|                 {
201|                     $ErrorArray[12] = "none";
202|                     $ErrorArray[13] = "hidden";
203|                     $ErrorArray[14] = "none";
204|                     $ErrorArray[15] = "hidden";
205|                     $ErrorArray[16] = "none";
206|                     $ErrorArray[17] = "hidden";
207|                 }
208| 
209|                 if(!$Switch)
210|                 {
211|                     contactPage('Contact Error(s)', $serverScript, $firstName, $lastName, $email, $subject, $message, '', $ErrorArray, $hTok, $CapA, $CapB, $CapSign, $capAnswer);
212|                 }
213|                 else
214|                 {
215|                     sthHash($DBName, $host, $user, $pass, $table, $hTok, $firstName, $lastName, $email, $subject, $message, $serverScript, $CapA, $CapB, $CapSign, $capAnswer, $dev);
216|                     mailMode($firstName, $lastName, $email, $subject, $message, $dev);
217|                 }
218|             }
219|         }
220|         else if($_POST['reset'])
221|         {
222|             $hTok = createHash($DBName, $host, $user, $pass, $table);
223|             $CapA = CapRandNum(1,10);
224|             $CapB = CapRandNum(1,10);
225|             $CapSign = CapRandSign();
226|             contactPage('Contact', $serverScript, '', '', '', '', '', '', null, $hTok, $CapA, $CapB, $CapSign, '');
227|         }
228|         else
229|         {
230|             $BError = "<p>Sorry, buttons are broken. Please turn JavaScript back on.</p>";
231|             contactPage('Contact', $serverScript, $firstName, $lastName, $email, $subject, $message, $BError, null, $hTok, $CapA, $CapB, $CapSign, $capAnswer);
232|         }
233|     }
234| ?>
235| 
236| <?php
237| 
238| //Check if text box isn't empty
239| function validateNotEmpty($String)
240| {
241|     $Valid = false;
242| 
243|     if($String != "")
244|     {
245|         $Valid = true;
246|     }
247| 
248|     return $Valid;
249| }
250| 
251| //Check if email is valid
252| function validateEmail($Address)
253| {
254|     $Valid = false;
255| 
256|     //if(strpos($Address, '@') !== FALSE && strpos($Address, '.') !== FALSE)
257|     if(preg_match("/^[^@]*@[^@]*\.[^@]*$/", $Address))
258|     {
259|         $Valid = true;
260|     }
261| 
262|     return $Valid;
263| 
264| }
265| 
266| //Check if a subject is selected
267| function validateSubject($Subject)
268| {
269|     $Valid = false;
270| 
271|     if($Subject != "Select")
272|     {
273|         $Valid = true;
274|     }
275| 
276|     return $Valid;
277| }
278| 
279| ?>