Advertisement
Guest User

Untitled

a guest
May 29th, 2016
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 17.90 KB | None | 0 0
  1. <?php
  2.  
  3. $act = isset($_REQUEST['act']) ? $_REQUEST['act'] : die('error');
  4. $params = isset($_REQUEST['json']) ? json_decode($_REQUEST['json'], true) : array();
  5. $jsonBox = array();
  6. $error = array();
  7. $info = array();
  8. $exemple = array();
  9. $form = array();
  10. $host = $_SERVER['HTTP_HOST'];
  11. $ref = $_SERVER['HTTP_REFERER'];
  12.  
  13.  
  14. $form['form-1'] = array(
  15. 'fields' => array(
  16. 'name' => array(
  17. 'title' => 'Имя',
  18. 'validate' => array(
  19. 'preg' => '%[A-Z-a-zА-Яа-я\s]%',
  20. 'minlength' => '3',
  21. 'maxlength' => '35',
  22. ),
  23. 'messages' => array(
  24. 'preg' => 'Поле [ %1$s ] возможно содержит ошибку',
  25. 'minlength' => 'Минимальная длинна поля [ %1$s ] меньше допустимой - %2$s',
  26. 'maxlength' => 'Максимальная длинна поля [ %1$s ] превышает допустимую - %2$s',
  27. )
  28. ),
  29. 'tell' => array(
  30. 'title' => 'Телефон',
  31. 'validate' => array(
  32. 'preg' => "/^((8|\+)[\- ]?)?(\(?\d{3}\)?[\- ]?)?[\d\- ]{5,10}$/",
  33. 'minlength' => '5',
  34. ),
  35. 'messages' => array(
  36. 'preg' => 'Поле [ %1$s ] возможно содержит ошибку',
  37. 'minlength' => 'Минимальная длинна поля [ %1$s ] меньше допустимой - %2$s',
  38. )
  39. ),
  40. 'city' => array(
  41. 'title' => 'город',
  42. 'validate' => array(
  43. 'preg' => '%[A-Z-a-zА-Яа-я\s]%',
  44. 'minlength' => '3',
  45. 'maxlength' => '35',
  46. ),
  47. 'messages' => array(
  48. 'preg' => 'Поле [ %1$s ] возможно содержит ошибку',
  49. 'minlength' => 'Минимальная длинна поля [ %1$s ] меньше допустимой - %2$s',
  50. 'maxlength' => 'Максимальная длинна поля [ %1$s ] превышает допустимую - %2$s',
  51. )
  52. ),
  53. ),
  54. 'cfg' => array(
  55. 'charset' => 'utf-8',
  56. 'subject' => 'Тема письма',
  57. 'title' => 'Заголовок в теле письма',
  58. 'ajax' => true,
  59. 'validate' => true,
  60. 'from_email' => 'avto-vykup.in.ua',
  61. 'from_name' => 'Klient',
  62. 'to_email' => 'artem-event@ukr.net, e_murik@i.ua, artem-event@gmail.com',
  63. 'to_name' => 'noreply1, noreply2',
  64. 'geoip' => true,
  65. 'referer' => true,
  66. 'type' => 'html',
  67. 'tpl' => true,
  68. 'antispam' => 'email77',
  69. 'antispamjs' => 'address77',
  70. 'okay' => 'Сообщение отправлено - OK',
  71. 'fuck' => 'Сообщение отправлено - ERROR',
  72. 'spam' => 'Cпам робот',
  73. 'notify' => 'color-modal-textbox',
  74. 'usepresuf' => false
  75. )
  76. );
  77. $form['form-2'] = array(
  78. 'fields' => array(
  79. 'tell' => array(
  80. 'title' => 'Телефон',
  81. 'validate' => array(
  82. 'preg' => "/^((8|\+)[\- ]?)?(\(?\d{3}\)?[\- ]?)?[\d\- ]{5,10}$/",
  83. 'minlength' => '5',
  84. ),
  85. 'messages' => array(
  86. 'preg' => 'Поле [ %1$s ] возможно содержит ошибку',
  87. 'minlength' => 'Минимальная длинна поля [ %1$s ] меньше допустимой - %2$s',
  88. )
  89. ),
  90. 'brand' => array(
  91. 'title' => 'Марка',
  92. 'validate' => array(
  93. 'preg' => '%[A-Z-a-zА-Яа-я\s]%',
  94. 'minlength' => '3',
  95. 'maxlength' => '35',
  96. ),
  97. 'messages' => array(
  98. 'preg' => 'Поле [ %1$s ] возможно содержит ошибку',
  99. 'minlength' => 'Минимальная длинна поля [ %1$s ] меньше допустимой - %2$s',
  100. 'maxlength' => 'Максимальная длинна поля [ %1$s ] превышает допустимую - %2$s',
  101. )
  102. ),
  103. 'model' => array(
  104. 'title' => 'Модель',
  105. 'validate' => array(
  106. 'preg' => '%[A-Z-a-zА-Яа-я\s]%',
  107. 'minlength' => '3',
  108. 'maxlength' => '35',
  109. ),
  110. 'messages' => array(
  111. 'preg' => 'Поле [ %1$s ] возможно содержит ошибку',
  112. 'minlength' => 'Минимальная длинна поля [ %1$s ] меньше допустимой - %2$s',
  113. 'maxlength' => 'Максимальная длинна поля [ %1$s ] превышает допустимую - %2$s',
  114. )
  115. ),
  116. 'year' => array(
  117. 'title' => 'Год выпуска',
  118. 'validate' => array(
  119. 'preg' => "/^((8|\+)[\- ]?)?(\(?\d{3}\)?[\- ]?)?[\d\- ]{5,10}$/",
  120. 'minlength' => '5',
  121. ),
  122. 'messages' => array(
  123. 'preg' => 'Поле [ %1$s ] возможно содержит ошибку',
  124. 'minlength' => 'Минимальная длинна поля [ %1$s ] меньше допустимой - %2$s',
  125. )
  126. ),
  127. 'price' => array(
  128. 'title' => 'Цена',
  129. 'validate' => array(
  130. 'preg' => "/^((8|\+)[\- ]?)?(\(?\d{3}\)?[\- ]?)?[\d\- ]{5,10}$/",
  131. 'minlength' => '5',
  132. ),
  133. 'messages' => array(
  134. 'preg' => 'Поле [ %1$s ] возможно содержит ошибку',
  135. 'minlength' => 'Минимальная длинна поля [ %1$s ] меньше допустимой - %2$s',
  136. )
  137. ),
  138. 'checkbox' => array(
  139. 'title' => 'Коробка автомат',
  140. 'validate' => array(
  141. 'minlength' => '1',
  142. ),
  143. 'messages' => array(
  144. 'minlength' => '[ %1$s ] необходимо установить',
  145. )
  146. ),
  147. ),
  148. 'cfg' => array(
  149. 'charset' => 'utf-8',
  150. 'subject' => 'Тема письма',
  151. 'title' => 'Заголовок в теле письма',
  152. 'ajax' => true,
  153. 'validate' => true,
  154. 'from_email' => 'noreply@email.com',
  155. 'from_name' => 'noreply',
  156. 'to_email' => 'artem-event@ukr.net, e_murik@i.ua',
  157. 'to_name' => 'noreply1, noreply2',
  158. 'geoip' => true,
  159. 'referer' => true,
  160. 'type' => 'html',
  161. 'tpl' => true,
  162. 'antispam' => 'email77',
  163. 'antispamjs' => 'address77',
  164. 'okay' => 'Сообщение отправлено - OK',
  165. 'fuck' => 'Сообщение отправлено - ERROR',
  166. 'spam' => 'Cпам робот',
  167. 'notify' => 'color-modal-textbox',
  168. 'usepresuf' => false
  169. )
  170. );
  171. $form['form-3'] = array(
  172. 'fields' => array(
  173. 'name' => array(
  174. 'title' => 'Имя',
  175. 'validate' => array(
  176. 'preg' => '%[A-Z-a-zА-Яа-я\s]%',
  177. 'minlength' => '3',
  178. 'maxlength' => '35',
  179. ),
  180. 'messages' => array(
  181. 'preg' => 'Поле [ %1$s ] возможно содержит ошибку',
  182. 'minlength' => 'Минимальная длинна поля [ %1$s ] меньше допустимой - %2$s',
  183. 'maxlength' => 'Максимальная длинна поля [ %1$s ] превышает допустимую - %2$s',
  184. )
  185. ),
  186. 'tell' => array(
  187. 'title' => 'Телефон',
  188. 'validate' => array(
  189. 'preg' => "/^((8|\+)[\- ]?)?(\(?\d{3}\)?[\- ]?)?[\d\- ]{5,10}$/",
  190. 'minlength' => '5',
  191. ),
  192. 'messages' => array(
  193. 'preg' => 'Поле [ %1$s ] возможно содержит ошибку',
  194. 'minlength' => 'Минимальная длинна поля [ %1$s ] меньше допустимой - %2$s',
  195. )
  196. ),
  197. 'e-mail' => array(
  198. 'title' => 'E-mail',
  199. 'validate' => array(
  200. 'preg' => '%@%is',
  201. 'minlength' => '5',
  202. ),
  203. 'messages' => array(
  204. 'preg' => 'Поле [ %1$s ] возможно содержит ошибку',
  205. 'minlength' => 'Минимальная длинна поля [ %1$s ] меньше допустимой - %2$s',
  206. )
  207. ),
  208. 'text' => array(
  209. 'title' => 'Сообщение',
  210. 'validate' => array(
  211. 'minlength' => '1',
  212. ),
  213. 'messages' => array(
  214. 'minlength' => '[ %1$s ] необходимо заполнить',
  215. )
  216. ),
  217. 'checkbox' => array(
  218. 'title' => 'Checkbox',
  219. 'validate' => array(
  220. 'minlength' => '1',
  221. ),
  222. 'messages' => array(
  223. 'minlength' => '[ %1$s ] необходимо установить',
  224. )
  225. ),
  226. 'radio' => array(
  227. 'title' => 'Radio',
  228. 'validate' => array(
  229. 'minlength' => '1',
  230. ),
  231. 'messages' => array(
  232. 'minlength' => '[ %1$s ] необходимо выбрать',
  233. )
  234. ),
  235. 'select' => array(
  236. 'title' => 'Select',
  237. 'validate' => array(
  238. 'minlength' => '1',
  239. ),
  240. 'messages' => array(
  241. 'minlength' => '[ %1$s ] необходимо выбрать',
  242. )
  243. ),
  244. ),
  245. 'cfg' => array(
  246. 'charset' => 'utf-8',
  247. 'subject' => 'Тема письма',
  248. 'title' => 'Заголовок в теле письма',
  249. 'ajax' => true,
  250. 'validate' => true,
  251. 'from_email' => 'noreply@email.com',
  252. 'from_name' => 'noreply',
  253. 'to_email' => 'noreply1@email.com, noreply2@email.com',
  254. 'to_name' => 'noreply1, noreply2',
  255. 'geoip' => true,
  256. 'referer' => true,
  257. 'type' => 'plain',
  258. 'tpl' => false,
  259. 'antispam' => 'email77',
  260. 'antispamjs' => 'address77',
  261. 'okay' => 'Сообщение отправлено - OK',
  262. 'fuck' => 'Сообщение отправлено - ERROR',
  263. 'spam' => 'Cпам робот',
  264. 'notify' => 'color-modal',
  265. 'usepresuf' => false
  266. )
  267. );
  268.  
  269.  
  270.  
  271.  
  272. if($act == 'cfg') {
  273. $jsonBox['configs'] = ExportConfigs($form);
  274. die(json_encode($jsonBox));
  275. }
  276.  
  277. function ExportConfigs($form) {
  278. $need = array('antispam','antispamjs','notify');
  279. $conf = array();
  280. foreach($form as $name => $data) {
  281. foreach($data['cfg'] as $k => $cfg) {
  282. if(in_array($k, $need)) {
  283. $conf[$name]['cfg'][$k] = $cfg;
  284. }
  285. }
  286. }
  287.  
  288. return $conf;
  289. }
  290.  
  291.  
  292. if(isset($form[$act])) {
  293.  
  294. $form = $form[$act];
  295. $getdata = array();
  296. $sb = array(); // subject и body
  297.  
  298.  
  299. foreach($form['fields'] as $name => $field) {
  300.  
  301. $title = (isset($field['title'])) ? $field['title'] : $name;
  302. $getdata[$name]['title'] = $title;
  303. $rawdata = isset($_POST[$name]) ? trim($_POST[$name]) : '';
  304.  
  305. if(isset($field['validate'])) {
  306.  
  307. $def = 'Поле с именем [ '.$name.' ] содержит ошибку.';
  308. // -0-
  309. if(isset($field['validate']['required']) &&
  310. empty($rawdata)) {
  311. $error[$name] = isset($field['messages']['required']) ? sprintf($field['messages']['required'], $title) :
  312. (isset($messages['validator']['required']) ? sprintf($messages['validator']['required'], $title) : $def);
  313. }
  314. // -1-
  315. if(isset($field['validate']['minlength']) &&
  316. mb_strlen($rawdata) < $field['validate']['minlength']) {
  317. $error[$name] = isset($field['messages']['minlength']) ? sprintf($field['messages']['minlength'], $title, $field['validate']['minlength']) : $def;
  318. }
  319. // -2-
  320. if(isset($field['validate']['maxlength']) &&
  321. mb_strlen($rawdata) > $field['validate']['maxlength']) {
  322. $error[$name] = isset($field['messages']['maxlength']) ? sprintf($field['messages']['maxlength'], $title, $field['validate']['maxlength']) : $def;
  323. }
  324. // -3-
  325. if(isset($field['validate']['preg']) && mb_strlen($rawdata) > 0 &&
  326. !preg_match($field['validate']['preg'], $rawdata)) {
  327. $error[$name] = isset($field['messages']['preg']) ? sprintf($field['messages']['preg'], $title, $field['validate']['preg']) : $def;
  328. }
  329. // -4-
  330. if(isset($field['validate']['substr']) &&
  331. mb_strlen($rawdata) > $field['validate']['substr']) {
  332. $rawdata = mb_substr($rawdata, 0, $field['validate']['substr']);
  333. }
  334.  
  335. $outdata = htmlspecialchars($rawdata);
  336.  
  337. $getdata[$name]['value'] = $outdata;
  338.  
  339. }
  340. else {
  341. $getdata[$name]['value'] = htmlspecialchars($rawdata);
  342. }
  343.  
  344. if(empty($getdata[$name]['value'])) {
  345. unset($getdata[$name]);
  346. }
  347.  
  348.  
  349. } //foreach end
  350.  
  351.  
  352. if(isset($form['cfg']['antispam']) && isset($_POST[$form['cfg']['antispam']])) {
  353. if(!empty($_POST[$form['cfg']['antispam']])) {
  354. $error[] = $form['cfg']['spam'];
  355. }
  356. }
  357. if(isset($form['cfg']['antispamjs']) && isset($_POST[$form['cfg']['antispamjs']])) {
  358. if(!empty($_POST[$form['cfg']['antispamjs']])) {
  359. $error[] = $form['cfg']['spam'];
  360. }
  361. }
  362.  
  363.  
  364. if(count($error) == 0) {
  365.  
  366. if(function_exists("mb_internal_encoding")) mb_internal_encoding($form['cfg']['charset']);
  367. $get_fromName = (isset($form['fields'][$form['cfg']['from_name']]) && isset($getdata[$form['cfg']['from_name']]['value']) && mb_strlen($getdata[$form['cfg']['from_name']]['value']) > 2) ? $getdata[$form['cfg']['from_name']]['value'] : ((mb_strlen($form['cfg']['from_name']) > 2 && !isset($_POST[$form['cfg']['from_name']])) ? $form['cfg']['from_name'] : 'Anonymous');
  368. $get_fromEmail = (isset($form['fields'][$form['cfg']['from_email']]) && isset($getdata[$form['cfg']['from_email']]['value']) && mb_strpos('@', $getdata[$form['cfg']['from_email']]['value']) === false) ? $getdata[$form['cfg']['from_email']]['value'] : ((mb_strpos('@', $form['cfg']['from_email']) !== false) ? $form['cfg']['from_email'] : 'no-reply@'.$host);
  369.  
  370. $fromName = (function_exists("mb_encode_mimeheader")) ? mb_encode_mimeheader($get_fromName, $form['cfg']['charset'], "Q") : $get_fromName;
  371. $sb['subject'] = (function_exists("mb_encode_mimeheader")) ? mb_encode_mimeheader($form['cfg']['subject'], $form['cfg']['charset'], "Q") : $form['cfg']['subject'];
  372.  
  373. $toName = trim($form['cfg']['to_name'], " ,");
  374. $toEmail = trim($form['cfg']['to_email'], " ,");
  375.  
  376. if(strpos($toName, ",") !== false) {
  377. $exp_toName = explode(",", $toName);
  378. $c = count($exp_toName);
  379. for($i=0; $i<$c; $i++) {
  380. $exp_toName[$i] = (function_exists("mb_encode_mimeheader")) ? mb_encode_mimeheader(trim($exp_toName[$i]), $form['cfg']['charset'], "Q") : trim($exp_toName[$i]);
  381. }
  382. }
  383. else {
  384. $toName = (function_exists("mb_encode_mimeheader")) ? mb_encode_mimeheader($toName, $form['cfg']['charset'], "Q") : $toName;
  385. }
  386.  
  387. if(strpos($toEmail, ",") !== false) {
  388. $exp_toEmail = explode(",", $toEmail);
  389. }
  390.  
  391. $To = '';
  392.  
  393. if(isset($exp_toEmail)) {
  394. $c = count($exp_toEmail);
  395. for($i=0; $i < $c; $i++) {
  396. $To .= ((isset($exp_toName) && isset($exp_toName[$i])) ? $exp_toName[$i] : $toName) . " <".trim($exp_toEmail[$i]).">";
  397. if($i < ($c-1)) $To .= ", ";
  398. }
  399. }
  400. else {
  401. $To = ((isset($exp_toName) && isset($exp_toName[0])) ? $exp_toName[0] : $toName)." <".$toEmail.">";
  402. }
  403.  
  404.  
  405. $headers = "Return-Path: <".$get_fromEmail.">\r\n";
  406. $headers .= "From: ".$fromName." <".$get_fromEmail.">\r\n";
  407. $headers .= "X-Mailer: Feedback, v0.3 (http://artuelle.com)\r\n";
  408. $headers .= "X-Priority: 3\r\n";
  409. $headers .= "Reply-To: ".$fromName." <".$get_fromEmail.">\r\n";
  410. //$headers .= "To: ".$To."\r\n";
  411. $headers .= "MIME-Version: 1.0\r\n";
  412. $headers .= "Content-Type: text/" . $form['cfg']['type'] . "; charset=\"" . $form['cfg']['charset'] . "\"\r\n";
  413. $headers .= "Content-Transfer-Encoding: 8bit\r\n";
  414.  
  415. $sb['body'] = "";
  416. // парсим шаблон
  417. if($form['cfg']['tpl']) {
  418. $out = tpl(array('name' => $act, 'getdata' => $getdata, 'cfg' => $form['cfg']));
  419. if(is_string($out)) {
  420. $sb['body'] = $out;
  421. }
  422. }
  423. // или отдаем голый текст
  424. if(mb_strlen(trim($sb['body'])) < 10) {
  425. if(isset($form['cfg']['title']))
  426. $sb['body'] .= $form['cfg']['title']."\r\n\r\n";
  427. foreach($getdata as $name => $data) {
  428. $sb['body'] .= $data['title'].": ".$data['value']."\r\n";
  429. }
  430. if($form['cfg']['referer'])
  431. $sb['body'] .= "\r\n\r\n\r\n\r\n".$ref;
  432. }
  433. // если есть что добавить
  434. if(isset($form['cfg']['adds']) &&
  435. is_array($form['cfg']['adds'])) {
  436. $sb = adds($sb);
  437. }
  438.  
  439. //отправка письма
  440. $mail = mail($To, $sb['subject'], $sb['body'], $headers);
  441.  
  442. if($mail) {
  443. $jsonBox['ok'] = 1;
  444. $info[] = $form['cfg']['okay'];
  445.  
  446. //setcookie("limit", "1", time() + $form['cfg']['limit']);
  447.  
  448. }
  449. else {
  450. $info[] = $form['cfg']['fuck'];
  451. }
  452.  
  453. }
  454.  
  455. } else {
  456. $error[] = 'Нет настроек формы с именем #'.$act;
  457. }
  458.  
  459. if(count($error) > 0) {
  460. $jsonBox['errors'] = $error;
  461. }
  462. if(count($info) > 0) {
  463. $jsonBox['infos'] = $info;
  464. }
  465.  
  466. die(json_encode($jsonBox)); //мертвые с косами
  467.  
  468. /* добавляет любые доп данные из вне в нужное место
  469. * (например в заголовок письма необходимо дабавить Ник юзера или Номер заказа )
  470. *
  471. * */
  472.  
  473. function adds($vars) {
  474. global $form;
  475. $adds = $form['cfg']['adds'];
  476. foreach($adds as $key => $opts) {
  477. if(is_string($key)) {
  478. $one = array();
  479. $two = array();
  480. foreach($opts as $i => $val) {
  481. if(isset($_POST[$val])) {
  482. $one[] = '%%'.$val.'%%';
  483. $two[] = $_POST[$val];
  484. }
  485. }
  486. $vars[$key] = str_replace($one, $two, $vars[$key]);
  487. }
  488. }
  489. return $vars;
  490. }
  491. /*
  492. * парсер шаблона
  493. */
  494. function tpl($vars) {
  495. $tpl = 'tpl/'.$vars['name'].'.tpl';
  496. if(file_exists($tpl)) {
  497. $template = file_get_contents($tpl);
  498. foreach($vars['getdata'] as $name => $data) {
  499. $template = str_replace(array("%%".$name.".title%%", "%%".$name.".value%%"), array($data['title'], $data['value']), $template);
  500. }
  501. return $template;
  502. }
  503. else {
  504. return false;
  505. }
  506. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement