Posted By

enfotoad on 12/29/10


Tagged


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

geldora


Tiny Contact Form PHP


 / Published in: PHP
 

URL: http://www.motivosolutions.com/wp-admin/plugin-editor.php

  1. <?php
  2. /*
  3. Plugin Name: Tiny Contact Form
  4. Plugin URI: http://www.tomsdimension.de/wp-plugins/tiny-contact-form
  5. Description: Little form that allows site visitors to contact you. Use [TINY-CONTACT-FORM] within any post or page.
  6. Author: Tom Braider
  7. Author URI: http://www.tomsdimension.de
  8. Version: 0.7
  9. */
  10.  
  11. $tcf_version = '0.7';
  12. $tcf_script_printed = 0;
  13. $tiny_contact_form = new TinyContactForm();
  14.  
  15. class TinyContactForm
  16. {
  17.  
  18. var $o; // options
  19. var $captcha;
  20. var $userdata;
  21. var $nr = 0; // form number to use more then once forms/widgets
  22.  
  23.  
  24. /**
  25.  * Constructor
  26.  */
  27. function TinyContactForm()
  28. {
  29. // get options from DB
  30. $this->o = get_option('tiny_contact_form');
  31. // widget
  32. add_action('widgets_init', array( &$this, 'register_widgets'));
  33. // options page in menu
  34. add_action('admin_menu', array( &$this, 'addOptionsPage'));
  35. // shortcode
  36. add_shortcode('TINY-CONTACT-FORM', array( &$this, 'shortcode'));
  37. // add stylesheet
  38. add_action('wp_head', array( &$this, 'addStyle'));
  39. // uninstall function
  40. if ( function_exists('register_uninstall_hook') )
  41. register_uninstall_hook(ABSPATH.PLUGINDIR.'/tiny-contact-form/tiny-contact-form.php', array( &$this, 'uninstall'));
  42. // settingslink on plugin page
  43. add_filter('plugin_action_links', array( &$this, 'pluginActions'), 10, 2);
  44. // add locale support
  45. if (defined('WPLANG') && function_exists('load_plugin_textdomain'))
  46. load_plugin_textdomain('tcf-lang', false, 'tiny-contact-form/locale');
  47. // creates image recources
  48. $this->setRecources();
  49. }
  50.  
  51. /**
  52.  * creates tcf code
  53.  *
  54.  * @return string form code
  55.  */
  56. function showForm( $params = '' )
  57. {
  58. $n = ($this->nr == 0) ? '' : $this->nr;
  59. $this->nr++;
  60.  
  61. if ( isset($_POST['tcf_sender'.$n]) )
  62. $result = $this->sendMail( $n, $params );
  63.  
  64. $captcha = new TinyContactFormCaptcha( rand(1000000000, 9999999999) );
  65.  
  66. $form = '<div class="contactform" id="tcform'.$n.'">';
  67.  
  68. if ( !empty($result) )
  69. {
  70. if ( $result == $this->o['msg_ok'] )
  71. // mail successfully sent, no form
  72. $form .= '<p class="contactform_respons">'.$result.'</p>';
  73. else
  74. // error message
  75. $form .= '<p class="contactform_error">'.$result.'</p>';
  76. }
  77.  
  78. if ( empty($result) || (!empty($result) && !$this->o['hideform']) )
  79. {
  80. // subject from form
  81. if ( !empty($_POST['tcf_subject'.$n]) )
  82. $tcf_subject = $_POST['tcf_subject'.$n];
  83. // subject from widget instance
  84. else if ( is_array($params) && !empty($params['subject']))
  85. $tcf_subject = $params['subject'];
  86. // subject from URL
  87. else if ( empty($_POST['tcf_subject'.$n]) && !empty($_GET['subject']) )
  88. $tcf_subject = $_GET['subject'];
  89. // subject from shortcode
  90. else if ( empty($_POST['tcf_subject'.$n]) && !empty($this->userdata['subject']) )
  91. $tcf_subject = $this->userdata['subject'];
  92. else
  93. $tcf_subject = '';
  94.  
  95. $tcf_sender = (isset($_POST['tcf_sender'.$n])) ? $_POST['tcf_sender'.$n] : '';
  96. $tcf_email = (isset($_POST['tcf_email'.$n])) ? $_POST['tcf_email'.$n] : '';
  97. $tcf_msg = (isset($_POST['tcf_msg'.$n])) ? $_POST['tcf_msg'.$n] : '';
  98.  
  99. $form .= '
  100. <form action="#tcform'.$n.'" method="post" id="tinyform'.$n.'">
  101. <div>
  102. <input name="tcf_name'.$n.'" id="tcf_name'.$n.'" value="" class="tcf_input" />
  103. <input name="tcf_sendit'.$n.'" id="tcf_sendit'.$n.'" value="1" class="tcf_input" />
  104. <label for="tcf_sender'.$n.'" class="tcf_label">'.__('Name', 'tcf-lang').':</label>
  105. <input name="tcf_sender'.$n.'" id="tcf_sender'.$n.'" size="30" value="'.$tcf_sender.'" class="tcf_field" />
  106. <label for="tcf_email'.$n.'" class="tcf_label">'.__('Email', 'tcf-lang').':</label>
  107. <input name="tcf_email'.$n.'" id="tcf_email'.$n.'" size="30" value="'.$tcf_email.'" class="tcf_field" />';
  108. // additional fields
  109. for ( $x = 1; $x <=5; $x++ )
  110. {
  111. $i = 'tcf_field_'.$x.$n;
  112. $tcf_f = (isset($_POST[$i])) ? $_POST[$i] : '';
  113. $f = $this->o['field_'.$x];
  114. if ( !empty($f) )
  115. $form .= '
  116. <label for="'.$i.'" class="tcf_label">'.$f.':</label>
  117. <input name="'.$i.'" id="'.$i.'" size="30" value="'.$tcf_f.'" class="tcf_field" />';
  118. }
  119. $form .= '
  120. <label for="tcf_subject'.$n.'" class="tcf_label">'.__('Subject', 'tcf-lang').':</label>
  121. <input name="tcf_subject'.$n.'" id="tcf_subject'.$n.'" size="30" value="'.$tcf_subject.'" class="tcf_field" />
  122. <label for="tcf_msg'.$n.'" class="tcf_label">'.__('Your Message', 'tcf-lang').':</label>
  123. <textarea name="tcf_msg'.$n.'" id="tcf_msg'.$n.'" class="tcf_textarea" cols="50" rows="10">'.$tcf_msg.'</textarea>
  124. ';
  125. if ( $this->o['captcha'] )
  126. $form .= $captcha->getCaptcha($n);
  127. if ( $this->o['captcha2'] )
  128. $form .= '
  129. <label for="tcf_captcha2_'.$n.'" class="tcf_label">'.$this->o['captcha2_question'].'</label>
  130. <input name="tcf_captcha2_'.$n.'" id="tcf_captcha2_'.$n.'" size="30" class="tcf_field" />
  131. ';
  132.  
  133. $title = (!empty($this->o['submit'])) ? 'value="'.$this->o['submit'].'"' : '';
  134. $form .= '
  135. <input type="submit" name="submit'.$n.'" id="contactsubmit'.$n.'" class="tcf_submit" '.$title.' onclick="return checkForm(\''.$n.'\');" />
  136. </div>
  137. </form>';
  138. }
  139.  
  140. $form .= '</div>';
  141. $form .= $this->addScript();
  142. return $form;
  143. }
  144.  
  145. /**
  146.  * adds javescript code to check the values
  147.  */
  148. function addScript()
  149. {
  150. global $tcf_script_printed;
  151. if ($tcf_script_printed) // only once
  152. return;
  153.  
  154. $script = "
  155. <script type=\"text/javascript\">
  156. //<![CDATA[
  157. function checkForm( n )
  158. {
  159. var f = new Array();
  160. f[1] = document.getElementById('tcf_sender' + n).value;
  161. f[2] = document.getElementById('tcf_email' + n).value;
  162. f[3] = document.getElementById('tcf_subject' + n).value;
  163. f[4] = document.getElementById('tcf_msg' + n).value;
  164. f[5] = f[6] = f[7] = f[8] = f[9] = '-';
  165. ";
  166. for ( $x = 1; $x <=5; $x++ )
  167. if ( !empty($this->o['field_'.$x]) )
  168. $script .= 'f['.($x + 4).'] = document.getElementById("tcf_field_'.$x.'" + n).value;'."\n";
  169. $script .= '
  170. var msg = "";
  171. for ( i=0; i < f.length; i++ )
  172. {
  173. if ( f[i] == "" )
  174. msg = "'.__('Please fill out all fields.', 'tcf-lang').'\nPlease fill out all fields.\n\n";
  175. }
  176. if ( !isEmail(f[2]) )
  177. msg += "'.__('Wrong Email.', 'tcf-lang').'\nWrong Email.";
  178. if ( msg != "" )
  179. {
  180. alert(msg);
  181. return false;
  182. }
  183. }
  184. function isEmail(email)
  185. {
  186. var rx = /^([^\s@,:"<>]+)@([^\s@,:"<>]+\.[^\s@,:"<>.\d]{2,}|(\d{1,3}\.){3}\d{1,3})$/;
  187. var part = email.match(rx);
  188. if ( part )
  189. return true;
  190. else
  191. return false
  192. }
  193. //]]>
  194. </script>
  195. ';
  196. $tcf_script_printed = 1;
  197. return $script;
  198. }
  199.  
  200. /**
  201.  * send mail
  202.  *
  203.  * @return string Result, Message
  204.  */
  205. function sendMail( $n = '', $params = '' )
  206. {
  207. $result = $this->checkInput( $n );
  208.  
  209. if ( $result == 'OK' )
  210. {
  211. $result = '';
  212.  
  213. // use "to" from widget instance
  214. if ( is_array($params) && !empty($params['to']))
  215. $to = $params['to'];
  216. // or from shortcode
  217. else if ( !empty($this->userdata['to']) )
  218. $to = $this->userdata['to'];
  219. // or default
  220. else
  221. $to = $this->o['to_email'];
  222.  
  223. $from = $this->o['from_email'];
  224.  
  225. $name = $_POST['tcf_sender'.$n];
  226. $email = $_POST['tcf_email'.$n];
  227. $subject= $this->o['subpre'].' '.$_POST['tcf_subject'.$n];
  228. $msg = $_POST['tcf_msg'.$n];
  229.  
  230. // additional fields
  231. $extra = '';
  232. foreach ($_POST as $k => $f )
  233. if ( strpos( $k, 'tcf_field_') !== false )
  234. $extra .= $this->o[substr($k, 4, 7)].": $f
  235. ";
  236.  
  237. // create mail
  238. $headers =
  239. "MIME-Version: 1.0
  240. ".
  241. "Reply-To: \"$name\" <$email>
  242. ".
  243. "Content-Type: text/plain; charset=\"".get_settings('blog_charset')."\"
  244. ";
  245. if ( !empty($from) )
  246. $headers .= "From: ".get_bloginfo('name')." - $name <$from>
  247. ";
  248. else if ( !empty($email) )
  249. $headers .= "From: ".get_bloginfo('name')." - $name <$email>
  250. ";
  251.  
  252. $fullmsg =
  253. "Name: $name
  254. ".
  255. "Email: $email
  256. ".
  257. $extra."
  258. ".
  259. 'Subject: '.$_POST['tcf_subject'.$n]."
  260.  
  261. ".
  262. wordwrap($msg, 76, "
  263. ")."
  264.  
  265. ".
  266. 'Referer: '.$_SERVER['HTTP_REFERER']."
  267. ".
  268. 'Browser: '.$_SERVER['HTTP_USER_AGENT']."
  269. ";
  270.  
  271. // send mail
  272. if ( wp_mail( $to, $subject, $fullmsg, $headers) )
  273. {
  274. // ok
  275. if ( $this->o['hideform'] )
  276. {
  277. unset($_POST['tcf_sender'.$n]);
  278. unset($_POST['tcf_email'.$n]);
  279. unset($_POST['tcf_subject'.$n]);
  280. unset($_POST['tcf_msg'.$n]);
  281. foreach ($_POST as $k => $f )
  282. if ( strpos( $k, 'tcf_field_') !== false )
  283. unset($k);
  284. }
  285. $result = $this->o['msg_ok'];
  286. }
  287. else
  288. // error
  289. $result = $this->o['msg_err'];
  290. }
  291. return $result;
  292. }
  293.  
  294. /**
  295.  * shows options page
  296.  */
  297. function optionsPage()
  298. {
  299. global $tcf_version;
  300. if (!current_user_can('manage_options'))
  301. wp_die(__('Sorry, but you have no permissions to change settings.'));
  302.  
  303. // save data
  304. if ( isset($_POST['tcf_save']) )
  305. {
  306. $to = stripslashes($_POST['tcf_to_email']);
  307. if ( empty($to) )
  308. $to = get_option('admin_email');
  309. $msg_ok = stripslashes($_POST['tcf_msg_ok']);
  310. if ( empty($msg_ok) )
  311. $msg_ok = "Thank you! Your message was sent successfully.";
  312. $msg_err = stripslashes($_POST['tcf_msg_err']);
  313. if ( empty($msg_err) )
  314. $msg_err = "Sorry. An error occured while sending the message!";
  315. $captcha = ( isset($_POST['tcf_captcha']) ) ? 1 : 0;
  316. $captcha2 = ( isset($_POST['tcf_captcha2']) ) ? 1 : 0;
  317. $hideform = ( isset($_POST['tcf_hideform']) ) ? 1 : 0;
  318.  
  319. $this->o = array(
  320. 'to_email' => $to,
  321. 'from_email' => stripslashes($_POST['tcf_from_email']),
  322. 'css' => stripslashes($_POST['tcf_css']),
  323. 'msg_ok' => $msg_ok,
  324. 'msg_err' => $msg_err,
  325. 'submit' => stripslashes($_POST['tcf_submit']),
  326. 'captcha' => $captcha,
  327. 'captcha_label' => stripslashes($_POST['tcf_captcha_label']),
  328. 'captcha2' => $captcha2,
  329. 'captcha2_question' => stripslashes($_POST['tcf_captcha2_question']),
  330. 'captcha2_answer' => stripslashes($_POST['tcf_captcha2_answer']),
  331. 'subpre' => stripslashes($_POST['tcf_subpre']),
  332. 'field_1' => stripslashes($_POST['tcf_field_1']),
  333. 'field_2' => stripslashes($_POST['tcf_field_2']),
  334. 'field_3' => stripslashes($_POST['tcf_field_3']),
  335. 'field_4' => stripslashes($_POST['tcf_field_4']),
  336. 'field_5' => stripslashes($_POST['tcf_field_5']),
  337. 'hideform' => $hideform
  338. );
  339. update_option('tiny_contact_form', $this->o);
  340. }
  341.  
  342. // show page
  343. ?>
  344. <div id="poststuff" class="wrap">
  345. <h2><img src="<?php echo $this->getResource('tcf_logo.png') ?>" alt="" style="width:24px;height:24px" /> Tiny Contact Form</h2>
  346. <div class="postbox">
  347. <h3><?php _e('Options', 'cpd') ?></h3>
  348. <div class="inside">
  349.  
  350. <form action="options-general.php?page=tiny-contact-form" method="post">
  351. <table class="form-table">
  352. <tr>
  353. <td colspan="2" style="border-top: 1px #ddd solid; background: #eee"><strong><?php _e('Form', 'tcf-lang'); ?></strong></td>
  354. </tr>
  355. <tr>
  356. <th><?php _e('TO:', 'tcf-lang')?></th>
  357. <td><input name="tcf_to_email" type="text" size="70" value="<?php echo $this->o['to_email'] ?>" /><br /><?php _e('E-mail'); ?>, <?php _e('one or more (e.g. email1,email2,email3)', 'tcf-lang'); ?></td>
  358. </tr>
  359. <tr>
  360. <th><?php _e('FROM:', 'tcf-lang')?> <?php _e('(optional)', 'tcf-lang'); ?></th>
  361. <td><input name="tcf_from_email" type="text" size="70" value="<?php echo $this->o['from_email'] ?>" /><br /><?php _e('E-mail'); ?></td>
  362. </tr>
  363. <tr>
  364. <th><?php _e('Message OK:', 'tcf-lang')?></th>
  365. <td><input name="tcf_msg_ok" type="text" size="70" value="<?php echo $this->o['msg_ok'] ?>" /></td>
  366. </tr>
  367. <tr>
  368. <th><?php _e('Message Error:', 'tcf-lang')?></th>
  369. <td><input name="tcf_msg_err" type="text" size="70" value="<?php echo $this->o['msg_err'] ?>" /></td>
  370. </tr>
  371. <tr>
  372. <th><?php _e('Submit Button:', 'tcf-lang')?> <?php _e('(optional)', 'tcf-lang'); ?></th>
  373. <td><input name="tcf_submit" type="text" size="70" value="<?php echo $this->o['submit'] ?>" /></td>
  374. </tr>
  375. <tr>
  376. <th><?php _e('Subject Prefix:', 'tcf-lang')?> <?php _e('(optional)', 'tcf-lang'); ?></th>
  377. <td><input name="tcf_subpre" type="text" size="70" value="<?php echo $this->o['subpre'] ?>" /></td>
  378. </tr>
  379. <tr>
  380. <th><?php _e('Additional Fields:', 'tcf-lang')?></th>
  381. <td>
  382. <p><?php _e('The contact form includes the fields Name, Email, Subject and Message. If you need more (e.g. Phone, Website) type in the name of the field.', 'tcf-lang'); ?></p>
  383. <?php
  384. for ( $x = 1; $x <= 5; $x++ )
  385. echo '<p>'.__('Field', 'tcf-lang').' '.$x.': <input name="tcf_field_'.$x.'" type="text" size="30" value="'.$this->o['field_'.$x].'" /></p>';
  386. ?>
  387. </td>
  388. </tr>
  389. <tr>
  390. <th><?php _e('After Submit', 'tcf-lang')?>:</th>
  391. <td><label for="tcf_hideform"><input name="tcf_hideform" id="tcf_hideform" type="checkbox" <?php if($this->o['hideform']==1) echo 'checked="checked"' ?> /> <?php _e('hide the form', 'tcf-lang'); ?></label></td>
  392. </tr>
  393. <tr>
  394. <td colspan="2" style="border-top: 1px #ddd solid; background: #eee"><strong><?php _e('Captcha', 'tcf-lang'); ?></strong></td>
  395. </tr>
  396. <tr>
  397. <th><?php _e('Captcha', 'tcf-lang')?>:</th>
  398. <td><label for="tcf_captcha"><input name="tcf_captcha" id="tcf_captcha" type="checkbox" <?php if($this->o['captcha']==1) echo 'checked="checked"' ?> /> <?php _e('use a little mathematic spam test like " 12 + 5 = "', 'tcf-lang'); ?></label></td>
  399. </tr>
  400. <tr>
  401. <th><?php _e('Captcha Label:', 'tcf-lang')?></th>
  402. <td><input name="tcf_captcha_label" type="text" size="70" value="<?php echo $this->o['captcha_label'] ?>" /></td>
  403. </tr>
  404. <tr style="border-top: 1px #ddd dashed;" >
  405. <th><?php _e('Alternative Captcha:', 'tcf-lang')?></th>
  406. <td><label for="tcf_captcha2"><input name="tcf_captcha2" id="tcf_captcha2" type="checkbox" <?php if($this->o['captcha2']==1) echo 'checked="checked"' ?> /> <?php _e('Set you own question and answer.', 'tcf-lang'); ?></label></td>
  407. </tr>
  408. <tr>
  409. <th><?php _e('Question:', 'tcf-lang')?></th>
  410. <td><input name="tcf_captcha2_question" type="text" size="70" value="<?php echo $this->o['captcha2_question'] ?>" /></td>
  411. </tr>
  412. <tr>
  413. <th><?php _e('Answer:', 'tcf-lang')?></th>
  414. <td><input name="tcf_captcha2_answer" type="text" size="70" value="<?php echo $this->o['captcha2_answer'] ?>" /></td>
  415. </tr>
  416. <tr>
  417. <td colspan="2" style="border-top: 1px #ddd solid; background: #eee"><strong><?php _e('Style', 'tcf-lang'); ?></strong></td>
  418. </tr>
  419. <tr>
  420. <th>
  421. <?php _e('StyleSheet:', 'tcf-lang'); ?><br />
  422. <a href="javascript:resetCss();"><?php _e('reset', 'tcf-lang'); ?></a>
  423. </th>
  424. <td>
  425. <textarea name="tcf_css" id="tcf_css" style="width:100%" rows="10"><?php echo $this->o['css'] ?></textarea><br />
  426. <?php _e('Use this field or the <code>style.css</code> in your theme directory.', 'tcf-lang') ?>
  427. </td>
  428. </tr>
  429. </table>
  430. <p class="submit">
  431. <input name="tcf_save" class="button-primary" value="<?php _e('Save Changes'); ?>" type="submit" />
  432. </p>
  433. </form>
  434.  
  435. <script type="text/javascript">
  436. function resetCss()
  437. {
  438. css = ".contactform {}\n.contactform label {}\n.contactform input {}\n.contactform textarea {}\n"
  439. + ".contactform_respons {}\n.contactform_error {}\n.widget .contactform { /* same fields but in sidebar */ }";
  440. document.getElementById('tcf_css').value = css;
  441. }
  442. </script>
  443. </div>
  444. </div>
  445.  
  446. <div class="postbox">
  447. <h3><?php _e('Contact', 'tcf-lang') ?></h3>
  448. <div class="inside">
  449. <p>
  450. Tiny Contact Form: <code><?php echo $tcf_version ?></code><br />
  451. <?php _e('Bug? Problem? Question? Hint? Praise?', 'tcf-lang') ?><br />
  452. <?php printf(__('Write a comment on the <a href="%s">plugin page</a>.', 'tcf-lang'), 'http://www.tomsdimension.de/wp-plugins/tiny-contact-form'); ?><br />
  453. <?php _e('License') ?>: <a href="http://www.tomsdimension.de/postcards">Postcardware :)</a>
  454. </p>
  455. <p><a href="<?php echo get_bloginfo('wpurl').'/'.PLUGINDIR ?>/tiny-contact-form/readme.txt?KeepThis=true&amp;TB_iframe=true" title="Tiny Contact Form - Readme.txt" class="thickbox"><strong>Readme.txt</strong></a></p>
  456. </div>
  457. </div>
  458.  
  459. </div>
  460. <?php
  461. }
  462.  
  463. /**
  464.  * adds admin menu
  465.  */
  466. function addOptionsPage()
  467. {
  468. global $wp_version;
  469. $menutitle = '';
  470. if ( version_compare( $wp_version, '2.6.999', '>' ) )
  471. $menutitle = '<img src="'.$this->getResource('tcf_menu.png').'" alt="" /> ';
  472. $menutitle .= 'Tiny Contact Form';
  473. add_options_page('Tiny Contact Form', $menutitle, 9, 'tiny-contact-form', array( &$this, 'optionsPage'));
  474. }
  475.  
  476. /**
  477.  * parses parameters
  478.  *
  479.  * @param string $atts parameters
  480.  */
  481. function shortcode( $atts )
  482. {
  483. // e.g. [TINY-CONTENT-FORM to="[email protected]" subject="xyz"]
  484.  
  485. extract( shortcode_atts( array(
  486. 'to' => '',
  487. 'subject' => ''
  488. ), $atts) );
  489. $this->userdata = array(
  490. 'to' => $to,
  491. 'subject' => $subject
  492. );
  493. return $this->showForm();
  494. }
  495.  
  496. /**
  497.  * check input fields
  498.  *
  499.  * @return string message
  500.  */
  501. function checkInput( $n = '' )
  502. {
  503. // exit if no form data
  504. if ( !isset($_POST['tcf_sendit'.$n]))
  505. return false;
  506.  
  507. // hidden field check
  508. if ( (isset($_POST['tcf_sendit'.$n]) && $_POST['tcf_sendit'.$n] != 1)
  509. || (isset($_POST['tcf_name'.$n]) && $_POST['tcf_name'.$n] != '') )
  510. {
  511. return 'No Spam please!';
  512. }
  513.  
  514. // for captcha check
  515. $o = get_option('tiny_contact_form');
  516.  
  517. $_POST['tcf_sender'.$n] = stripslashes(trim($_POST['tcf_sender'.$n]));
  518. $_POST['tcf_email'.$n] = stripslashes(trim($_POST['tcf_email'.$n]));
  519. $_POST['tcf_subject'.$n] = stripslashes(trim($_POST['tcf_subject'.$n]));
  520. $_POST['tcf_msg'.$n] = stripslashes(trim($_POST['tcf_msg'.$n]));
  521. // extra felder
  522.  
  523. $error = array();
  524. if ( empty($_POST['tcf_sender'.$n]) )
  525. $error[] = __('Name', 'tcf-lang');
  526. if ( !is_email($_POST['tcf_email'.$n]) )
  527. $error[] = __('Email', 'tcf-lang');
  528. if ( empty($_POST['tcf_subject'.$n]) )
  529. $error[] = __('Subject', 'tcf-lang');
  530. if ( empty($_POST['tcf_msg'.$n]) )
  531. $error[] = __('Your Message', 'tcf-lang');
  532. if ( $o['captcha'] && !TinyContactFormCaptcha::isCaptchaOk() )
  533. $error[] = $this->o['captcha_label'];
  534. if ( $o['captcha2'] && ( empty($_POST['tcf_captcha2_'.$n]) || $_POST['tcf_captcha2_'.$n] != $o['captcha2_answer'] ) )
  535. $error[] = $this->o['captcha2_question'];
  536. if ( !empty($error) )
  537. return __('Check these fields:', 'tcf-lang').' '.implode(', ', $error);
  538.  
  539. return 'OK';
  540. }
  541.  
  542. /**
  543.  * clean up when uninstall
  544.  */
  545. function uninstall()
  546. {
  547. delete_option('tiny_contact_form');
  548. }
  549.  
  550. /**
  551.  * adds custom style to page
  552.  */
  553. function addStyle()
  554. {
  555. echo "\n<!-- Tiny Contact Form -->\n"
  556. ."<style type=\"text/css\">\n"
  557. .".tcf_input {display:none !important; visibility:hidden !important;}\n"
  558. .$this->o['css']."\n"
  559. ."</style>\n";
  560. }
  561.  
  562. /**
  563.  * adds an action link to the plugins page
  564.  */
  565. function pluginActions($links, $file)
  566. {
  567. if( $file == plugin_basename(__FILE__)
  568. && strpos( $_SERVER['SCRIPT_NAME'], '/network/') === false ) // not on network plugin page
  569. {
  570. $link = '<a href="options-general.php?page=tiny-contact-form">'.__('Settings').'</a>';
  571. array_unshift( $links, $link );
  572. }
  573. return $links;
  574. }
  575.  
  576. /**
  577.  * defines base64 encoded image recources
  578.  */
  579. function setRecources()
  580. {
  581. if ( isset($_GET['resource']) && !empty($_GET['resource']) )
  582. {
  583. # base64 encoding
  584. $resources = array(
  585. 'tcf_menu.png' =>
  586. 'iVBORw0KGgoAAAANSUhEUgAAAAwAAAAMCAYAAABWdVznAAAAAX'.
  587. 'NSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAgY0hSTQAAeiYA'.
  588. 'AICEAAD6AAAAgOgAAHUwAADqYAAAOpgAABdwnLpRPAAAABh0RV'.
  589. 'h0U29mdHdhcmUAUGFpbnQuTkVUIHYzLjM2qefiJQAAAEtJREFU'.
  590. 'KFNj9GD4/58BCnYwMDI2NDTA+TBxymiQDTAMMglkAz5Mum20t4'.
  591. 'GQm9HlwZ4k1iNgtWRpIMVZINewEusksDrkYEWOQFyGAABXBYxc'.
  592. 'mDNSvQAAAABJRU5ErkJggg==',
  593. 'tcf_logo.png' =>
  594. 'iVBORw0KGgoAAAANSUhEUgAAABgAAAAYCAYAAADgdz34AAAAAX'.
  595. 'NSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAgY0hSTQAAeiYA'.
  596. 'AICEAAD6AAAAgOgAAHUwAADqYAAAOpgAABdwnLpRPAAAABh0RV'.
  597. 'h0U29mdHdhcmUAUGFpbnQuTkVUIHYzLjM2qefiJQAAAHpJREFU'.
  598. 'SEtj9GD4/58BC9jBwMgIEm5oaMAqj00PVjGQBdgwTDHIAkow0Q'.
  599. '4ZvAppHkQ0t2Dwhi2xLqN5ENHcAmJ9OnjVUVIMEKMXXJjREsMt'.
  600. 'oHYYwxw9agHOkMUIIlpFNO1TEdCPrFBM7YQEN4+2FuAq7NDFyf'.
  601. 'YerS0AAHa/Vp9sTByIAAAAAElFTkSuQmCC');
  602.  
  603. if ( array_key_exists($_GET['resource'], $resources) )
  604. {
  605. $content = base64_decode($resources[ $_GET['resource'] ]);
  606. $lastMod = filemtime(__FILE__);
  607. $client = ( isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) ? $_SERVER['HTTP_IF_MODIFIED_SINCE'] : false );
  608. if (isset($client) && (strtotime($client) == $lastMod))
  609. {
  610. header('Last-Modified: '.gmdate('D, d M Y H:i:s', $lastMod).' GMT', true, 304);
  611. }
  612. else
  613. {
  614. header('Last-Modified: '.gmdate('D, d M Y H:i:s', $lastMod).' GMT', true, 200);
  615. header('Content-Length: '.strlen($content));
  616. header('Content-Type: image/' . substr(strrchr($_GET['resource'], '.'), 1) );
  617. echo $content;
  618. }
  619. }
  620. }
  621. }
  622.  
  623. /**
  624.  * gets image recource with given name
  625.  */
  626. function getResource( $resourceID ) {
  627. return trailingslashit( get_bloginfo('url') ).'?resource='.$resourceID;
  628. }
  629.  
  630.  
  631. /**
  632.  * calls widget class
  633.  */
  634. function register_widgets()
  635. {
  636. register_widget('TinyContactForm_Widget');
  637. }
  638.  
  639. } // TCF class
  640.  
  641.  
  642.  
  643.  
  644.  
  645. /**
  646.  * Captcha class
  647.  */
  648. class TinyContactFormCaptcha
  649. {
  650.  
  651. var $first;
  652. var $operation;
  653. var $second;
  654. var $answer;
  655. var $captcha_id;
  656.  
  657. /**
  658.  * creates captcha
  659.  * @param String $seed ID
  660.  */
  661. function TinyContactFormCaptcha( $seed )
  662. {
  663. $this->captcha_id = $seed;
  664. if ( $seed )
  665. srand($seed);
  666. $operation = rand(1, 4);
  667. switch ( $operation )
  668. {
  669. case 1:
  670. $this->operation = '+';
  671. $this->first = rand(1, 20);
  672. $this->second = rand(0, 20);
  673. $this->answer = $this->first + $this->second;
  674. break;
  675. case 2:
  676. $this->operation = '-';
  677. $this->first = rand(1, 20);
  678. $this->second = rand(0, ($this->first - 1));
  679. $this->answer = $this->first - $this->second;
  680. break;
  681. case 3:
  682. $this->operation = 'x';
  683. $this->first = rand(1, 10);
  684. $this->second = rand(1, 10);
  685. $this->answer = $this->first * $this->second;
  686. break;
  687. case 4:
  688. $this->operation = '/';
  689. $this->first = rand(1, 20);
  690. do
  691. {
  692. $this->second = rand(1, 20);
  693. }
  694. while (($this->first % $this->second) != 0);
  695. $this->answer = $this->first / $this->second;
  696. break;
  697. }
  698. }
  699.  
  700. /**
  701.  * returns answer
  702.  */
  703. function getAnswer()
  704. {
  705. return $this->answer;
  706. }
  707.  
  708. /**
  709.  * returns question
  710.  */
  711. function getQuestion()
  712. {
  713. return $this->first.' '.$this->operation.' '.$this->second.' = ';
  714. }
  715.  
  716. /**
  717.  * checks answer
  718.  */
  719. function isCaptchaOk()
  720. {
  721. $ok = true;
  722. // time and ID in form?
  723. if ($_POST[base64_encode(strrev('current_time'))] && $_POST[base64_encode(strrev('captcha'))])
  724. {
  725. // maximum 30 minutes to fill the form
  726. if ((time() - strrev(base64_decode($_POST[base64_encode(strrev('current_time'))]))) > 1800)
  727. $ok = false;
  728. // check answer
  729. $valid = new TinyContactFormCaptcha(strrev(base64_decode($_POST[base64_encode(strrev('captcha'))])));
  730. if ($_POST[base64_encode(strrev('answer'))] != $valid->getAnswer())
  731. $ok = false;
  732. }
  733. return $ok;
  734. }
  735.  
  736. /**
  737.  * creates input fields in form
  738.  */
  739. function getCaptcha( $n = '' )
  740. {
  741. global $tiny_contact_form;
  742. return '<input name="'.base64_encode(strrev('current_time')).'" type="hidden" value="'.base64_encode(strrev(time())).'" />'."\n"
  743. .'<input name="'.base64_encode(strrev('captcha')).'" type="hidden" value="'.base64_encode(strrev($this->captcha_id)).'" />'."\n"
  744. .'<label class="tcf_label" style="display:inline" for="tcf_captcha'.$n.'">'.$tiny_contact_form->o['captcha_label'].' <b>'.$this->getQuestion().'</b></label> <input id="tcf_captcha'.$n.'" name="'.base64_encode(strrev('answer')).'" type="text" size="2" />'."\n";
  745. }
  746.  
  747. } // captcha class
  748.  
  749.  
  750.  
  751. class TinyContactForm_Widget extends WP_Widget
  752. {
  753. var $fields = array('Title', 'Subject', 'To');
  754.  
  755. /**
  756. * constructor
  757. */
  758. function TinyContactForm_Widget() {
  759. parent::WP_Widget('tcform_widget', 'Tiny Contact Form', array('description' => 'Little Contact Form'));
  760. }
  761.  
  762. /**
  763. * display widget
  764. */
  765. function widget( $args, $instance)
  766. {
  767. global $tiny_contact_form;
  768. extract($args, EXTR_SKIP);
  769. $title = empty($instance['title']) ? '&nbsp;' : apply_filters('widget_title', $instance['title']);
  770. echo $before_widget;
  771. if ( !empty( $title ) )
  772. echo $before_title.$title.$after_title;
  773. echo $tiny_contact_form->showForm( $instance );
  774. echo $after_widget;
  775. }
  776.  
  777. /**
  778. * update/save function
  779. */
  780. function update( $new_instance, $old_instance )
  781. {
  782. $instance = $old_instance;
  783. foreach ( $this->fields as $f )
  784. $instance[strtolower($f)] = strip_tags($new_instance[strtolower($f)]);
  785. return $instance;
  786. }
  787.  
  788. /**
  789. * admin control form
  790. */
  791. function form( $instance )
  792. {
  793. $default = array('title' => 'Tiny Contact Form');
  794. $instance = wp_parse_args( (array) $instance, $default );
  795.  
  796. foreach ( $this->fields as $field )
  797. {
  798. $f = strtolower( $field );
  799. $field_id = $this->get_field_id( $f );
  800. $field_name = $this->get_field_name( $f );
  801. echo "
  802. ".'<p><label for="'.$field_id.'">'.__($field, 'tcf-lang').': <input type="text" class="widefat" id="'.$field_id.'" name="'.$field_name.'" value="'.attribute_escape( $instance[$f] ).'" /><label></p>';
  803. }
  804. }
  805. } // widget class
  806. ?>

Report this snippet  

You need to login to post a comment.