網頁

2013年8月22日 星期四

php push_notification android

Android Push Notifications using Google Cloud Messaging (GCM), PHP and MySQL

5.  GCM.php 

Sending Android Push Notifications from PHP to phonegap applications


 
   
/*
 * Google API Key
 */  
define("GOOGLE_API_KEY", "AI088yCb858-eo6jdL1OmRvpwQCfYP5pHm0Q-3A"); // Place your Google API Key
 
   
/*
 *發送推播
 */
function android_push_notification($deviceToken,$message)
{
    include_once './GCM.php';
     
    $gcm = new GCM();
 
    $registatoin_ids = array($deviceToken);
    $message_array = array("message" => $message);
 
    $result = $gcm->send_notification($registatoin_ids, $message_array);

    return $result;

}

2013年8月19日 星期一

php push_apns iphone

siriusdely push_apns.php 改良資料庫與多線程
 
      $passphrase = 'XXX'; 

      // Put your alert message here:
      $message = 'A push notification has been sent!';

    $timeout = 1; //1sec

    $ctx = stream_context_create();
    stream_context_set_option($ctx, 'ssl', 'local_cert', 'smap_pt_pn_key.pem');
    stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);

    // Open a connection to the APNS server
    $fp = stream_socket_client('ssl://gateway.sandbox.push.apple.com:XXXX', $err, $errstr,$timeout, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx);
    //ssl://gateway.push.apple.com:XXXX


      $dbh = new PDO("mysql:host=" . db('hostname') . ";dbname=" . db('dbname'), db('username'), db('password'), 
      array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8") ); //POD處理中文的顯示的問題

      $sql_pn_device = "SELECT * FROM yourtablename WHERE user_id = '$user_id' ";

      $stmt = $dbh->prepare($sql_pn_device);
      $stmt->execute();
            
           if($stmt->fetchColumn() > 0) {

              foreach ($dbh->query($sql_pn_device) as $row) {
                        $deviceToken= $row['token'];
                         echo   $deviceToken."---send---".$message."|";
                     
                         push_apns($fp,$passphrase,$deviceToken,$message);
                   }
            } else {
                         echo  $user_id."---fail---";
            }
            
          // Close the connection to the server
          fclose($fp);

 

function push_apns($fp,$passphrase,$deviceToken,$message){


    //usleep(10);
    if (!$fp)
      exit("Failed to connect: $err $errstr" . PHP_EOL);

    echo 'Connected to APNS' . PHP_EOL;

    // Create the payload body
    $body['aps'] = array(
      'alert' => array(
            'body' => $message,
        'action-loc-key' => 'Bango App',
        ),
        'badge' => 2,
      'sound' => 'oven.caf',
      );

    // Encode the payload as JSON
    $payload = json_encode($body);

    // Build the binary notification
    $msg = chr(0) . pack('n', 32) . pack('H*', $deviceToken) . pack('n', strlen($payload)) . $payload;

    // Send it to the server
    $result = fwrite($fp, $msg, strlen($msg));
    echo "//".$result."//";
    if (!$result)
      echo 'Message not delivered' . PHP_EOL;
    else{
      echo 'Message successfully delivered' . PHP_EOL;
      //echo $message;
    }

phpmyadmin

資料庫重命名 點選操作標籤 / 編輯

2013年8月15日 星期四

WampServer Security Scanner

如何修復網站與作業系統常見之漏洞與弱點


AppScan偵測之漏洞: XSS Scripting 跨網站 Scripting

原程式加上php 提供之 function: htmlentities(),修改後之login_submit.php 內容: echo "您輸入的帳號: " . htmlentities($_POST['login']) . " 錯誤!";..


解決方法:
  

$password =  htmlentities($_POST['password']);  //Preventing XSS Scripting  Attacks

AppScan偵測之漏洞: SQL Injection SQL 注入

解決方法:
/etc/php.ini 有個參數: ; Magic quotes for incoming GET/POST/Cookie data. magic_quotes_gpc = On wamp/bin/php/php5.4.12/php.ini
; scheduled for removal in PHP 6.
; Default Value: On
; Development Value: Off
; Production Value: Off
; http://php.net/magic-quotes-gpc
magic_quotes_gpc = On ;LINE 750


當啟用 magic_quotes_gpc 參數後,對於GET/POST 中使用者輸入之特殊字元,如單引號(')雙引號( " )、反斜線( \ ), 前面會自動再加上反斜線( \ ),此時即視同為字元。當 magic_quotes_gpc 功能開啟後,SQL 執行之語法已改變為: SELECT * FROM admin where login='davis' and passwd='\' or \'a\'=\'a'

防止SQL注入攻擊 (mysql_real_escape_string) (轉)


AppScan偵測之漏洞:目錄清單瀏覽
需修改/etc/httpd/conf/httpd.conf 將 Options 中 Indexes 字串移除:
 

Options Indexes MultiViews
AllowOverride None
Order allow,deny
Allow from all
 

2013年8月14日 星期三

php JSON

你不可不知的 JSON 基本介紹

error_reporting(E_ALL^E_NOTICE^E_WARNING); //關閉PHP的所有錯誤提示
header('Content-Type: application/json; charset=utf-8'); //記得加utf-8 解決亂碼

require_once("db.config.php");


$jsonString = '
 {
  'action':"ok",
  "contents":[
        {
            "Name": "涼宮ハルヒン",
            "Attrib": "SOS団長"
        },
        {
            "Name": "キョン",
            "Attrib": "普通人"
        }
  ], "data":{ "Name": "涼宮ハルヒン","Attrib": "SOS団長"}
}
';

$arr_json = json_decode( $jsonString );
echo $arr_json->data->Name . "
";
涼宮ハルヒン
$arr_json = json_decode( $jsonString );


  $json_output .= "debug/>";
  $json_output .= '{';
  $json_output .= '"action" :"success" ,';
  //傳送參數回傳 
  $json_output .= '"params" : ';
  $json_output .= $jsonString  ; 
  $json_output .= ', ';
  //傳送參數回傳 
  $json_output .= ' "data" : "';
   // echo  $arr_json->contents[1]->user_share_loc_id ;  
       foreach ($arr_json->contents as $key => $value) {
            echo $arr_json->contents[$key]->Name."\n";
            echo $arr_json->contents[$key]->Attrib."\n";
           $json_output_data .=  $arr_json->contents[$key]->Name.", ";

  $json_output .= $json_output_data;
  $json_output .= '"}';
  echo $json_output;   
涼宮ハルヒン
SOS団長
キョン
普通人
debug/>{"action" :"success" ,"params" : 
 {
  "contents":[
        {
            "Name": "涼宮ハルヒン",
            "Attrib": "SOS団長"
        },
        {
            "Name": "キョン",
            "Attrib": "普通人"
        }
  ]
}
,  "data" : "涼宮ハルヒン, キョン, "}
若為抓取頁面 切割過濾JSON外的方法
        $output = explode("debug/>",  $pageContents);
        $arr_json =json_decode($output[1]);
        $action = $arr_json->{'action'};
        echo  $action;
        echo  $arr_json->contents[0]->Name;
涼宮ハルヒン



Google Places API


https://maps.googleapis.com/maps/api/place/search/json?location=-33.8670522,151.1957362&radius=500&types=food&name=harbour&sensor=false&key=AddYourOwnKeyHere


{
    "debug_info": [],
    "html_attributions": [],
    "results": [
        {
            "geometry": {
                "location": {
                    "lat": 25.085571,
                    "lng": 121.564002
                }
            },
            "icon": "http://maps.gstatic.com/mapfiles/place_api/icons/cafe-71.png",
            "id": "3d7fbd4541ad66c611646f4f592fa12101a24170",
            "name": "肯達咖啡",
            "opening_hours": {
                "open_now": true
            },
            ...
]
       

        $url = "https://maps.googleapis.com/maps/api/place/search/json?location=25.091075,121.559834&sensor=false&radius=700&types=cafe&key=yourkey";
      $place_search_json_result = file_get_contents($url, false);
       $jsonContent =json_decode($place_search_json_result);

       echo  $jsonContent->results[0]->name ;

       $geo_result =  $jsonContent->results[0];
       echo  '(' ;
       echo  $geo_result->geometry->location->lat;
       echo  ',' ;
       echo  $geo_result->geometry->location->lng;
       echo  ')' ;
肯達咖啡(25.085571,121.564002) php取得json
       

    $pageContents = file_get_contents($url, false);
  $place_search_json_result = explode("debug/>",  $pageContents);
  $locations_db_json =$place_search_json_result[1];
php傳值json給js
  

          // var locations = [
      //       {
      //           latlng : new google.maps.LatLng(25.0336148,  120.56480220000003),
      //                 info : "hello"
            
      //       },
      //       {
      //           latlng : new google.maps.LatLng(25.0336148,  121.56480220000003),
      //             info : "world"
      //       }

      //   ];
     var locations = [
                    
           ];

          for (var i = 0; i < locations.length; i++) {  
          var marker = new google.maps.Marker({
              position: locations[i].latlng,
            //icon: pinkmarker,
            //shadow: shadow,
            map: map
          });



          google.maps.event.addListener(marker, 'click', (function(marker, i) {
            return function() {

              infowindow.setContent(locations[i].info);
              infowindow.open(map, marker);
            }
          })(marker, i));
        }     
google spreadsheets return json
 
header('Content-type: application/json; charset="utf-8"');

$google_sheet_api = 'https://spreadsheets.google.com/feeds/list/1IKeEX6Vbn-T8pCiwLqURgYf_GGojE198iXXIS93_bD4/od6/public/values?alt=json';

 $api_json = file_get_contents($google_sheet_api);

 $api_json_str_replace = str_replace("$","",$api_json);

 $arr_json = json_decode(stripslashes($api_json_str_replace));
 //echo  $arr_json->version;
 //echo  $arr_json->feed->xmlns;
 //echo  $arr_json->feed->title->type;
 //echo  $arr_json->feed->title->t; //工作表1
 //echo  $arr_json->feed->entry[0]->gsxname->t;
 //echo  $arr_json->feed->entry[0]->gsxlatitude->t.",";
 //echo  $arr_json->feed->entry[0]->gsxlongitude->t;

 
 $data =[];

       foreach ($arr_json->feed->entry as $key => $value) {

         $name = $arr_json->feed->entry[$key]->gsxname->t;
         $latitude = $arr_json->feed->entry[$key]->gsxlatitude->t;
         $longitude = $arr_json->feed->entry[$key]->gsxlongitude->t;
         $address = $arr_json->feed->entry[$key]->gsxaddress->t;
 

         $record_array_preson = array("id"=>$key,"name"=>$name,"latitude"=>$latitude,"longitude"=>$longitude,"address"=>$address); 
         array_push($data,  $record_array_preson);

      }
       $json_output .= json_encode($data) ;

   echo $json_output; 
[
  {
    "id": 0,
    "name": "家樂福重慶店",
    "latitude": "25.0591477",
    "longitude": "121.5138074",
    "address": "台北市大同區重慶北路二段171號"
  },
  {
    "id": 1,
    "name": "家樂福大直店",
    "latitude": "25.0824879",
    "longitude": "121.5580935",
    "address": "台北市中山區樂群三路218號"
  },
..
]