Advertisement
Guest User

OKWave8322505

a guest
Oct 26th, 2013
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 16.83 KB | None | 0 0
  1. <?php
  2.  
  3. /* http://qiita.com/items/c39b9ee695a5c2e74627 より引用ここから */
  4. define('FILTER_STRUCT_FORCE_ARRAY', 1);
  5. define('FILTER_STRUCT_TRIM', 2);
  6. define('FILTER_STRUCT_FULL_TRIM', 4);
  7. function filter_struct_utf8($type, array $default) {
  8.     static $is_recursive_static = false;
  9.     $is_recursive = $is_recursive_static;
  10.     if (!$is_recursive) {
  11.         $types = array(
  12.             INPUT_GET => $_GET,
  13.             INPUT_POST => $_POST,
  14.             INPUT_COOKIE => $_COOKIE,
  15.             INPUT_REQUEST => $_REQUEST,
  16.         );
  17.         $type = (int)$type;
  18.         if (!isset($types[$type])) {
  19.             throw new LogicException('unknown super global var type');
  20.         }
  21.         $var = $types[$type];
  22.         $is_recursive_static = true;
  23.     } else {
  24.         $var = $type;
  25.     }
  26.     $trim_chars = "\\0\x20\x09\x0a\x0d\x0b";
  27.     $full_trim_chars = "{$trim_chars}\xc2\xa0\xe3\x80\x80";
  28.     $trim_pattern = "/\A[{$trim_chars}]++|[{$trim_chars}]++\z/u";
  29.     $full_trim_pattern = "/\A[{$full_trim_chars}]++|[{$full_trim_chars}]++\z/u";
  30.     $ret = array();
  31.     foreach ($default as $key => $value) {
  32.         if (is_int($value) && !($value & (
  33.             FILTER_STRUCT_FORCE_ARRAY |
  34.             FILTER_STRUCT_FULL_TRIM |
  35.             FILTER_STRUCT_TRIM
  36.         ))) {
  37.             if (!$is_recursive) {
  38.                 $is_recursive_static = false;
  39.             }
  40.             throw new LogicException('unknown bitmask');
  41.         }
  42.         if (is_int($value) && $value & FILTER_STRUCT_FORCE_ARRAY) {
  43.             $tmp = array();
  44.             if (isset($var[$key])) {
  45.                 foreach ((array)$var[$key] as $k => $v) {
  46.                     if (!preg_match('//u', $k)) {
  47.                         continue;
  48.                     }
  49.                     $value &= FILTER_STRUCT_FULL_TRIM | FILTER_STRUCT_TRIM;
  50.                     $tmp += array($k => $value ? $value : '');
  51.                 }
  52.             }
  53.             $value = $tmp;
  54.         }
  55.         if (isset($var[$key]) && is_array($value)) {
  56.             $ret[$key] = filter_struct_utf8($var[$key], $value);
  57.         } elseif (!isset($var[$key]) || is_array($var[$key])) {
  58.             $ret[$key] = is_int($value) ? '' : $value;
  59.         } else {
  60.             if (!isset($var[$key]) || is_array($var[$key])) {
  61.                 $var[$key] = null;
  62.             } elseif (is_int($value) && $value & FILTER_STRUCT_FULL_TRIM) {
  63.                 $var[$key] = preg_replace($full_trim_pattern, '', $var[$key]);
  64.             } elseif (is_int($value) && $value & FILTER_STRUCT_TRIM) {
  65.                 $var[$key] = preg_replace($trim_pattern, '', $var[$key]);
  66.             } else {
  67.                 $var[$key] = preg_replace('//u', '', $var[$key]);
  68.             }
  69.             if ($var[$key] === null) {
  70.                 $var[$key] = is_int($value) ? '' : $value;
  71.             }
  72.             $ret[$key] = $var[$key];
  73.         }
  74.     }
  75.     if (!$is_recursive) {
  76.         $is_recursive_static = false;
  77.     }
  78.     return $ret;
  79. }
  80. /* 引用ここまで */
  81.  
  82. /* http://qiita.com/mpyw/items/431c0c8cb70084a74be5 より一部引用ここから */
  83. function validate_phone_number($input, $strict = false) {
  84.     $groups = array(
  85.         5 =>
  86.         array (
  87.             '01564' => 1,
  88.             '01558' => 1,
  89.             '01586' => 1,
  90.             '01587' => 1,
  91.             '01634' => 1,
  92.             '01632' => 1,
  93.             '01547' => 1,
  94.             '05769' => 1,
  95.             '04992' => 1,
  96.             '04994' => 1,
  97.             '01456' => 1,
  98.             '01457' => 1,
  99.             '01466' => 1,
  100.             '01635' => 1,
  101.             '09496' => 1,
  102.             '08477' => 1,
  103.             '08512' => 1,
  104.             '08396' => 1,
  105.             '08388' => 1,
  106.             '08387' => 1,
  107.             '08514' => 1,
  108.             '07468' => 1,
  109.             '01655' => 1,
  110.             '01648' => 1,
  111.             '01656' => 1,
  112.             '01658' => 1,
  113.             '05979' => 1,
  114.             '04996' => 1,
  115.             '01654' => 1,
  116.             '01372' => 1,
  117.             '01374' => 1,
  118.             '09969' => 1,
  119.             '09802' => 1,
  120.             '09912' => 1,
  121.             '09913' => 1,
  122.             '01398' => 1,
  123.             '01377' => 1,
  124.             '01267' => 1,
  125.             '04998' => 1,
  126.             '01397' => 1,
  127.             '01392' => 1,
  128.         ),
  129.         4 =>
  130.         array (
  131.             '0768' => 2,
  132.             '0770' => 2,
  133.             '0772' => 2,
  134.             '0774' => 2,
  135.             '0773' => 2,
  136.             '0767' => 2,
  137.             '0771' => 2,
  138.             '0765' => 2,
  139.             '0748' => 2,
  140.             '0747' => 2,
  141.             '0746' => 2,
  142.             '0826' => 2,
  143.             '0749' => 2,
  144.             '0776' => 2,
  145.             '0763' => 2,
  146.             '0761' => 2,
  147.             '0766' => 2,
  148.             '0778' => 2,
  149.             '0824' => 2,
  150.             '0797' => 2,
  151.             '0796' => 2,
  152.             '0555' => 2,
  153.             '0823' => 2,
  154.             '0798' => 2,
  155.             '0554' => 2,
  156.             '0820' => 2,
  157.             '0795' => 2,
  158.             '0556' => 2,
  159.             '0791' => 2,
  160.             '0790' => 2,
  161.             '0779' => 2,
  162.             '0558' => 2,
  163.             '0745' => 2,
  164.             '0794' => 2,
  165.             '0557' => 2,
  166.             '0799' => 2,
  167.             '0738' => 2,
  168.             '0567' => 2,
  169.             '0568' => 2,
  170.             '0585' => 2,
  171.             '0586' => 2,
  172.             '0566' => 2,
  173.             '0564' => 2,
  174.             '0565' => 2,
  175.             '0587' => 2,
  176.             '0584' => 2,
  177.             '0581' => 2,
  178.             '0572' => 2,
  179.             '0574' => 2,
  180.             '0573' => 2,
  181.             '0575' => 2,
  182.             '0576' => 2,
  183.             '0578' => 2,
  184.             '0577' => 2,
  185.             '0569' => 2,
  186.             '0594' => 2,
  187.             '0827' => 2,
  188.             '0736' => 2,
  189.             '0735' => 2,
  190.             '0725' => 2,
  191.             '0737' => 2,
  192.             '0739' => 2,
  193.             '0743' => 2,
  194.             '0742' => 2,
  195.             '0740' => 2,
  196.             '0721' => 2,
  197.             '0599' => 2,
  198.             '0561' => 2,
  199.             '0562' => 2,
  200.             '0563' => 2,
  201.             '0595' => 2,
  202.             '0596' => 2,
  203.             '0598' => 2,
  204.             '0597' => 2,
  205.             '0744' => 2,
  206.             '0852' => 2,
  207.             '0956' => 2,
  208.             '0955' => 2,
  209.             '0954' => 2,
  210.             '0952' => 2,
  211.             '0957' => 2,
  212.             '0959' => 2,
  213.             '0966' => 2,
  214.             '0965' => 2,
  215.             '0964' => 2,
  216.             '0950' => 2,
  217.             '0949' => 2,
  218.             '0942' => 2,
  219.             '0940' => 2,
  220.             '0930' => 2,
  221.             '0943' => 2,
  222.             '0944' => 2,
  223.             '0948' => 2,
  224.             '0947' => 2,
  225.             '0946' => 2,
  226.             '0967' => 2,
  227.             '0968' => 2,
  228.             '0987' => 2,
  229.             '0986' => 2,
  230.             '0985' => 2,
  231.             '0984' => 2,
  232.             '0993' => 2,
  233.             '0994' => 2,
  234.             '0997' => 2,
  235.             '0996' => 2,
  236.             '0995' => 2,
  237.             '0983' => 2,
  238.             '0982' => 2,
  239.             '0973' => 2,
  240.             '0972' => 2,
  241.             '0969' => 2,
  242.             '0974' => 2,
  243.             '0977' => 2,
  244.             '0980' => 2,
  245.             '0979' => 2,
  246.             '0978' => 2,
  247.             '0920' => 2,
  248.             '0898' => 2,
  249.             '0855' => 2,
  250.             '0854' => 2,
  251.             '0853' => 2,
  252.             '0553' => 2,
  253.             '0856' => 2,
  254.             '0857' => 2,
  255.             '0863' => 2,
  256.             '0859' => 2,
  257.             '0858' => 2,
  258.             '0848' => 2,
  259.             '0847' => 2,
  260.             '0835' => 2,
  261.             '0834' => 2,
  262.             '0833' => 2,
  263.             '0836' => 2,
  264.             '0837' => 2,
  265.             '0846' => 2,
  266.             '0845' => 2,
  267.             '0838' => 2,
  268.             '0865' => 2,
  269.             '0866' => 2,
  270.             '0892' => 2,
  271.             '0889' => 2,
  272.             '0887' => 2,
  273.             '0893' => 2,
  274.             '0894' => 2,
  275.             '0897' => 2,
  276.             '0896' => 2,
  277.             '0895' => 2,
  278.             '0885' => 2,
  279.             '0884' => 2,
  280.             '0869' => 2,
  281.             '0868' => 2,
  282.             '0867' => 2,
  283.             '0875' => 2,
  284.             '0877' => 2,
  285.             '0883' => 2,
  286.             '0880' => 2,
  287.             '0879' => 2,
  288.             '0829' => 2,
  289.             '0550' => 2,
  290.             '0228' => 2,
  291.             '0226' => 2,
  292.             '0225' => 2,
  293.             '0224' => 2,
  294.             '0229' => 2,
  295.             '0233' => 2,
  296.             '0237' => 2,
  297.             '0235' => 2,
  298.             '0234' => 2,
  299.             '0223' => 2,
  300.             '0220' => 2,
  301.             '0192' => 2,
  302.             '0191' => 2,
  303.             '0187' => 2,
  304.             '0193' => 2,
  305.             '0194' => 2,
  306.             '0198' => 2,
  307.             '0197' => 2,
  308.             '0195' => 2,
  309.             '0238' => 2,
  310.             '0240' => 2,
  311.             '0260' => 2,
  312.             '0259' => 2,
  313.             '0258' => 2,
  314.             '0257' => 2,
  315.             '0261' => 2,
  316.             '0263' => 2,
  317.             '0266' => 2,
  318.             '0265' => 2,
  319.             '0264' => 2,
  320.             '0256' => 2,
  321.             '0255' => 2,
  322.             '0243' => 2,
  323.             '0242' => 2,
  324.             '0241' => 2,
  325.             '0244' => 2,
  326.             '0246' => 2,
  327.             '0254' => 2,
  328.             '0248' => 2,
  329.             '0247' => 2,
  330.             '0186' => 2,
  331.             '0185' => 2,
  332.             '0144' => 2,
  333.             '0143' => 2,
  334.             '0142' => 2,
  335.             '0139' => 2,
  336.             '0145' => 2,
  337.             '0146' => 2,
  338.             '0154' => 2,
  339.             '0153' => 2,
  340.             '0152' => 2,
  341.             '0138' => 2,
  342.             '0137' => 2,
  343.             '0125' => 2,
  344.             '0124' => 2,
  345.             '0123' => 2,
  346.             '0126' => 2,
  347.             '0133' => 2,
  348.             '0136' => 2,
  349.             '0135' => 2,
  350.             '0134' => 2,
  351.             '0155' => 2,
  352.             '0156' => 2,
  353.             '0176' => 2,
  354.             '0175' => 2,
  355.             '0174' => 2,
  356.             '0178' => 2,
  357.             '0179' => 2,
  358.             '0184' => 2,
  359.             '0183' => 2,
  360.             '0182' => 2,
  361.             '0173' => 2,
  362.             '0172' => 2,
  363.             '0162' => 2,
  364.             '0158' => 2,
  365.             '0157' => 2,
  366.             '0163' => 2,
  367.             '0164' => 2,
  368.             '0167' => 2,
  369.             '0166' => 2,
  370.             '0165' => 2,
  371.             '0267' => 2,
  372.             '0250' => 2,
  373.             '0533' => 2,
  374.             '0422' => 2,
  375.             '0532' => 2,
  376.             '0531' => 2,
  377.             '0436' => 2,
  378.             '0428' => 2,
  379.             '0536' => 2,
  380.             '0299' => 2,
  381.             '0294' => 2,
  382.             '0293' => 2,
  383.             '0475' => 2,
  384.             '0295' => 2,
  385.             '0297' => 2,
  386.             '0296' => 2,
  387.             '0495' => 2,
  388.             '0438' => 2,
  389.             '0466' => 2,
  390.             '0465' => 2,
  391.             '0467' => 2,
  392.             '0478' => 2,
  393.             '0476' => 2,
  394.             '0470' => 2,
  395.             '0463' => 2,
  396.             '0479' => 2,
  397.             '0493' => 2,
  398.             '0494' => 2,
  399.             '0439' => 2,
  400.             '0268' => 2,
  401.             '0480' => 2,
  402.             '0460' => 2,
  403.             '0538' => 2,
  404.             '0537' => 2,
  405.             '0539' => 2,
  406.             '0279' => 2,
  407.             '0548' => 2,
  408.             '0280' => 2,
  409.             '0282' => 2,
  410.             '0278' => 2,
  411.             '0277' => 2,
  412.             '0269' => 2,
  413.             '0270' => 2,
  414.             '0274' => 2,
  415.             '0276' => 2,
  416.             '0283' => 2,
  417.             '0551' => 2,
  418.             '0289' => 2,
  419.             '0287' => 2,
  420.             '0547' => 2,
  421.             '0288' => 2,
  422.             '0544' => 2,
  423.             '0545' => 2,
  424.             '0284' => 2,
  425.             '0291' => 2,
  426.             '0285' => 2,
  427.             '0120' => 3,
  428.             '0570' => 3,
  429.             '0800' => 3,
  430.             '0990' => 3,
  431.         ),
  432.         3 =>
  433.         array (
  434.             '099' => 3,
  435.             '054' => 3,
  436.             '058' => 3,
  437.             '098' => 3,
  438.             '095' => 3,
  439.             '097' => 3,
  440.             '052' => 3,
  441.             '053' => 3,
  442.             '011' => 3,
  443.             '096' => 3,
  444.             '049' => 3,
  445.             '015' => 3,
  446.             '048' => 3,
  447.             '072' => 3,
  448.             '084' => 3,
  449.             '028' => 3,
  450.             '024' => 3,
  451.             '076' => 3,
  452.             '023' => 3,
  453.             '047' => 3,
  454.             '029' => 3,
  455.             '075' => 3,
  456.             '025' => 3,
  457.             '055' => 3,
  458.             '026' => 3,
  459.             '079' => 3,
  460.             '082' => 3,
  461.             '027' => 3,
  462.             '078' => 3,
  463.             '077' => 3,
  464.             '083' => 3,
  465.             '022' => 3,
  466.             '086' => 3,
  467.             '089' => 3,
  468.             '045' => 3,
  469.             '044' => 3,
  470.             '092' => 3,
  471.             '046' => 3,
  472.             '017' => 3,
  473.             '093' => 3,
  474.             '059' => 3,
  475.             '073' => 3,
  476.             '019' => 3,
  477.             '087' => 3,
  478.             '042' => 3,
  479.             '018' => 3,
  480.             '043' => 3,
  481.             '088' => 3,
  482.             '050' => 4,
  483.         ),
  484.         2 =>
  485.         array (
  486.             '04' => 4,
  487.             '03' => 4,
  488.             '06' => 4,
  489.         ),
  490.     );
  491.     $groups[3] +=
  492.         $strict ?
  493.         array(
  494.             '020' => 3,
  495.             '070' => 3,
  496.             '080' => 3,
  497.             '090' => 3,
  498.         ) :
  499.         array(
  500.             '020' => 4,
  501.             '070' => 4,
  502.             '080' => 4,
  503.             '090' => 4,
  504.         )
  505.     ;
  506.     $number = preg_replace('/[^\d]++/', '', $input);
  507.     foreach ($groups as $len => $group) {
  508.         $area = substr($number, 0, $len);
  509.         if (isset($group[$area])) {
  510.             $formatted = implode('-', array(
  511.                 $area,
  512.                 substr($number, $len, $group[$area]),
  513.                 substr($number, $len + $group[$area])
  514.             ));
  515.             return strrchr($formatted, '-') !== '-';
  516.         }
  517.     }
  518.     $pattern = '/\A(00(?:[013-8]|2\d|91[02-9])\d)(\d++)\z/';
  519.     return (bool)preg_match($pattern, $number, $matches);
  520. }
  521. /* 引用ここまで */
  522.  
  523.  
  524.  
  525.  
  526.  
  527.  
  528.  
  529.  
  530.  
  531. // 文字化け防止のためにテキストとしてのヘッダー出力
  532. header('Content-Type: text/plain; charset=utf8');
  533.  
  534. // POSTされた変数をフィルタリング
  535. $p = filter_struct_utf8(INPUT_POST, array(
  536.     'name' => FILTER_STRUCT_FULL_TRIM, // 両端をトリミングする
  537.     'org' => FILTER_STRUCT_FULL_TRIM, // 両端をトリミングする
  538.     'addr' => FILTER_STRUCT_FULL_TRIM, // 両端をトリミングする
  539.     'tel' => FILTER_STRUCT_FULL_TRIM, // 両端をトリミングする
  540.     'mail' => FILTER_STRUCT_FULL_TRIM, // 両端をトリミングする
  541.     'course' => '',
  542.     'number' => '',
  543. ));
  544.  
  545. // エラー格納配列
  546. $errors = array();
  547.  
  548. // 名前チェック
  549. if ($p['name'] === '') {
  550.     $errors[] = '名前が未入力です';
  551. }
  552.  
  553. // 所属組織チェック
  554. if ($p['org'] === '') {
  555.     $errors[] = '所属組織が未入力です';
  556. }
  557.  
  558. // 連絡住所チェック
  559. if ($p['addr'] === '') {
  560.     $errors[] = '連絡住所が未入力です';
  561. }
  562.  
  563. // 電話番号チェック
  564. if ($p['tel'] === '') {
  565.     $errors[] = '電話番号が未入力です';
  566. } elseif (!validate_phone_number($p['tel'])) {
  567.     $errors[] = '電話番号が不正です';
  568. }
  569.  
  570. // メルアドチェック
  571. if ($p['mail'] === '') {
  572.     $errors[] = 'メルアドが未入力です';
  573. } elseif (filter_var($p['mail'], FILTER_VALIDATE_EMAIL) === false) {
  574.     $errors[] = 'メルアドが不正です';
  575. }
  576.  
  577. // コースチェック
  578. if (!in_array(
  579.     $p['course'],
  580.     array('Aコースドリンク', 'Bコースドリンク', 'Cコースワイン'),
  581.     true
  582. )) {
  583.     $errors[] = 'コースを選択してください';
  584. }
  585.  
  586. // 人数チェック
  587. if (!ctype_digit($p['number']) || $p['number'] < 1 || $p['number'] > 10) {
  588.     $errors[] = '人数を選択してください';
  589. }
  590.  
  591. // エラーが1つでもあれば終了
  592. if ($errors) {
  593.     die(implode("\n", $errors));
  594. }
  595.  
  596. // データベースとの通信
  597. try {
  598.    
  599.     // PDOオブジェクト生成
  600.     $pdo = new PDO(
  601.         'mysql:dbname=xmas;host=localhost;charset=utf8',
  602.         'root',
  603.         'dPZ3Y8vXrP9NNb6',
  604.         array(
  605.             PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
  606.         )
  607.     );
  608.    
  609.     // プリペアドステートメントを生成
  610.     $stmt = $pdo->prepare(implode(' ', array(
  611.         'INSERT',
  612.         'INTO `xmastran`(`name`, `org`, `addr`, `tel`, `mail`, `course`, `number`)',
  613.         'VALUES (:name, :org, :addr, :tel, :mail, :course, :number)'
  614.     )));
  615.    
  616.     // 変数をバインドして実行
  617.     $stmt->execute($p);
  618.    
  619.     echo "登録成功(代表者:{$name})";
  620.    
  621. } catch (PDOException $e) {
  622.    
  623.     echo $e->getMessage();
  624.    
  625. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement