yms/validate-functions.php
changeset 0 9997bee9ad03
child 10 351d40b21cbc
equal deleted inserted replaced
-1:000000000000 0:9997bee9ad03
       
     1 <?php
       
     2 
       
     3 function yms_send_reply($result, $api_key = '', $extra = array())
       
     4 {
       
     5   header('Content-type: text/plain');
       
     6   
       
     7   global $g_api_key;
       
     8   
       
     9   if ( empty($api_key) )
       
    10     $api_key = $g_api_key;
       
    11   
       
    12   if ( empty($api_key) )
       
    13     $api_key = base64_encode("\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00");
       
    14   
       
    15   $now = gmdate("Y-m-d\TH:i:s");
       
    16   echo yms_generate_signed_response(array_merge($extra, array(
       
    17       't' => $now,
       
    18       'status' => $result
       
    19     )), $api_key);
       
    20   
       
    21   exit;
       
    22 }
       
    23 
       
    24 function yms_generate_signed_response($response, $api_key)
       
    25 {
       
    26   $hash = yms_val_sign($response, $api_key);
       
    27   $result = "h={$hash}\n";
       
    28   foreach ( $response as $key => $value )
       
    29   {
       
    30     $result .= "{$key}={$value}\n";
       
    31   }
       
    32   return trim($result);
       
    33 }
       
    34 
       
    35 function yms_val_sign($response, $api_key)
       
    36 {
       
    37   foreach ( array('h', 'title', 'auth') as $key )
       
    38     if ( isset($response[$key]) )
       
    39       unset($response[$key]);
       
    40     
       
    41   ksort($response);
       
    42   
       
    43   $signstr = array();
       
    44   foreach ( $response as $key => $value )
       
    45   {
       
    46     $signstr[] = "$key=$value";
       
    47   }
       
    48   
       
    49   $signstr = implode('&', $signstr);
       
    50   
       
    51   $api_key = yms_hex_encode(base64_decode($api_key));
       
    52   $hash = hmac_sha1($signstr, $api_key);
       
    53   $hash = yms_hex_decode($hash);
       
    54   $hash = base64_encode($hash);
       
    55   
       
    56   return $hash;
       
    57 }