1.

<?php
require 'Slim/Slim.php';
require 'DBManagement.php';
\Slim\Slim::registerAutoloader(); $app = new \Slim\Slim(); //Get specific location
$app->get("/locations/:user/:password/:id/:inuse/:targetfound", function($user, $password, $id, $inuse, $targetFound) use($app)
{
$type = authenticate_user($user, $password); if(connect($type))
{
if(strtolower($id) == "all")
{
if(strtolower($inuse) == "true")
{ if(strtolower($targetFound) == "true")
{
$sql = "SELECT * FROM locations WHERE ID in (Select LocationID from missions where TargetsDetected > 0)";
}
else
{
$sql = "SELECT * FROM locations where ID in (Select LocationID from missions)";
} }
else
{
$sql = "SELECT * FROM locations";
if(strtolower($targetFound) == "true")
{
$sql = $sql." WHERE ID in (Select LocationID from missions where TargetsDetected > 0)";
}
}
}else
{
$sql = "SELECT * FROM locations where ID = $id";
if(strtolower($targetFound) == "true")
{
$sql = $sql." and ID in (Select LocationID from missions where TargetsDetected > 0)";
}
} $result = mysql_query($sql);
$locations = array(); while($row = mysql_fetch_array($result))
{
$locations[] = array("ID"=> $row['id'],
"LocationName"=> $row['LocationName'],
"MinX" => $row['MinX'],
"MinY" => $row['MinY'],
"MaxX"=>$row['MaxX'],
"MaxY"=>$row['MaxY']);
} $app->response()->header("Content-Type", "application/json");
echo json_encode($locations); close_connection();
}else
{
$app->response()->status(401);
$app->response()->header("Content-Type", "text/plain");
}
}); //Get specific PilotID
$app->get("/pilot/:user/:password", function($user, $password) use($app)
{
$type = authenticate_user($user, $password); if(connect($type))
{ $result = mysql_query("SELECT * FROM users where UserName = '$user' and Password = '$password' and Enabled = 1");
$pilot = array(); while($row = mysql_fetch_array($result))
{
$pilot[] = array("ID"=> $row['ID'],
"UserName"=> $row['UserName'],
"FirstName"=> $row['FirstName'],
"LastName"=> $row['LastName'],
"Description"=>$row['Description'],
"IsAdmin"=>$row['IsAdmin']);
} $app->response()->header("Content-Type", "application/json");
echo json_encode($pilot); close_connection();
}else
{
$app->response()->status(401);
$app->response()->header("Content-Type", "text/plain");
}
}); //Get airframes
$app->get("/airframes/:user/:password", function($user, $password) use($app)
{
$type = authenticate_user($user, $password); if(connect($type))
{ $result = mysql_query("SELECT * FROM aircraft");
$aircraft = array(); while($row = mysql_fetch_array($result))
{
$aircraft[] = array("ID"=> $row['ID'],
"PlaneName"=> $row['PlaneName'],
"Description"=> $row['Description']);
} $app->response()->header("Content-Type", "application/json");
echo json_encode($aircraft); close_connection();
}else
{
$app->response()->status(401);
$app->response()->header("Content-Type", "text/plain");
}
}); //Get cameras
$app->get("/cameras/:user/:password", function($user, $password) use($app)
{
$type = authenticate_user($user, $password); if(connect($type))
{ $result = mysql_query("SELECT * FROM cameras");
$cameras = array(); while($row = mysql_fetch_array($result))
{
$cameras[] = array("ID"=> $row['ID'],
"Model"=> $row['Model'],
"HorizontalRes"=> $row['HorizontalRes'],
"VerticalRes"=> $row['VerticalRes'],
"FocalLength"=> $row['FocalLength']);
} $app->response()->header("Content-Type", "application/json");
echo json_encode($cameras); close_connection();
}else
{
$app->response()->status(401);
$app->response()->header("Content-Type", "text/plain");
}
}); //Get target types
$app->get("/targettypes/:user/:password", function($user, $password) use($app)
{
$type = authenticate_user($user, $password); if(connect($type))
{ $result = mysql_query("SELECT * FROM target_types");
$targets = array(); while($row = mysql_fetch_array($result))
{
$targets[] = array("ID"=> $row['ID'],
"TargetName"=> $row['TargetName'],
"Description"=> $row['Description']);
} $app->response()->header("Content-Type", "application/json");
echo json_encode($targets); close_connection();
}else
{
$app->response()->status(401);
$app->response()->header("Content-Type", "text/plain");
}
}); //Get number of flights since date and beaches covered
$app->get("/missions/flightstats/:user/:password/:earliest", function($user, $password, $earliest) use($app)
{
$type = authenticate_user($user, $password); if(connect($type))
{
//get recent sightings first
$sql = "SELECT TargetTypeID, MAX( MissionID ) AS max_mission, MAX( DateRecorded ) AS date_recorded
FROM mission_points
WHERE targettypeid >1
GROUP BY targettypeid
ORDER BY date_recorded DESC";
$result = mysql_query($sql);
$topTargets = array(); if($result)
{
while($row = mysql_fetch_array($result))
{ $sql = "SELECT LocationID from missions where ID = ".$row['max_mission'];
$locationResult = mysql_query($sql);
if($locationResult)
{
$locationRow = mysql_fetch_array($locationResult); $topTargets[] = array("TargetTypeID"=>$row['TargetTypeID'],
"MissionID"=>$row['max_mission'],
"DateRecorded"=>$row['date_recorded'],
"LocationID"=>$locationRow['LocationID']);
}
} //now get flight stats $sql = "SELECT Count(ID) as MissionCount, sum(DistanceFlown) as TotalDistance from missions"; if($earliest != strtolower("all"))
{
$sql = $sql. " where DateFlown >= '$earliest';";
} $result = mysql_query($sql);
$stats = array();
if($result)
{
$mCount = "0";
$totalDist = "0";
$beaches = "0"; while($row = mysql_fetch_array($result))
{
$mCount = $row['MissionCount'];
$totalDist = $row['TotalDistance'];
} $sql = "SELECT Count(distinct LocationID) as LocationCount from missions";
if($earliest != "all")
{
$sql = $sql. " where DateFlown >= '$earliest';";
} $result = mysql_query($sql);
if($result)
{
while($row = mysql_fetch_array($result))
{
$beaches = $row['LocationCount'];
} $stats[] = array("MissionCount"=> $mCount,
"TotalDistance"=> $totalDist,
"BeachesCovered"=> $beaches,
"LatestTargets"=> $topTargets); $app->response()->header("Content-Type", "application/json");
echo json_encode($stats); }
else
{
$app->response()->status(400);
$app->response()->header("Content-Type", "text/plain");
echo("Error querying beaches");
}
}
else
{
$app->response()->status(400);
$app->response()->header("Content-Type", "text/plain");
echo("Error querying missions");
}
}
else
{
$app->response()->status(400);
$app->response()->header("Content-Type", "text/plain");
echo("Error querying beaches");
} close_connection();
}else
{
$app->response()->status(401);
$app->response()->header("Content-Type", "text/plain");
echo("Authenticaton error for user " + $user);
}
}); //Get most recent target sightings broken down by target type
$app->get("/missions/latesttargets/:user/:password", function($user, $password) use($app)
{
$type = authenticate_user($user, $password); if(connect($type))
{
$sql = "Select mp.TargetTypeID, m.LocationID, mp.MissionID, max(mp.DateRecorded) as date_recorded from mission_points as mp, missions as m where m.ID = mp.MissionID and targettypeid > 1 group by targettypeid order by date_recorded desc";
$result = mysql_query($sql);
if($result)
{
$topTargets = array();
while($row = mysql_fetch_array($result))
{
$topTargets[] = array("TargetTypeID"=>$row['TargetTypeID'],
"MissionID"=>$row['MissionID'],
"DateRecorded"=>$row['date_recorded'],
"LocationID"=>$row['LocationID']);
} $app->response()->header("Content-Type", "application/json");
echo json_encode($topTargets);
}else
{
$app->response()->status(400);
$app->response()->header("Content-Type", "text/plain");
echo("Error querying database");
} close_connection();
}else
{
$app->response()->status(401);
$app->response()->header("Content-Type", "text/plain");
echo("Authenticaton error for user " + $user);
}
}); //Get mission data
$app->get("/missions/:user/:password/:locationid/:earliest/:detected", function($user, $password, $locationID, $earliest, $targetDetected) use($app)
{
$type = authenticate_user($user, $password); if(connect($type))
{
$sql = "";
if(is_numeric($locationID))
{ if($locationID == 133 && strtolower($earliest) == "all")
{
$sql = "SELECT * FROM missions";
}
else if(strtolower($earliest) == "all")
{
$sql = "SELECT * FROM missions where LocationID = $locationID";
}
else if($locationID == 133)
{
$sql = "SELECT * FROM missions where DateFlown >= '$earliest'";
}
else
{
$sql = "SELECT * FROM missions where LocationID = $locationID and DateFlown >= '$earliest'";
}
$missions = array();
$result = mysql_query($sql);
while($row = mysql_fetch_array($result))
{
if(strtolower($targetDetected) == "false" ||
(strtolower($targetDetected) == "true" && $row['TargetsDetected'] > 0))
{
$missionPoints = array();
$missionPtsResult = mysql_query("SELECT * FROM mission_points where MissionID = ".$row['ID']); if($missionPtsResult)
{
while($pointsRow = mysql_fetch_array($missionPtsResult))
{
$missionPoints[] = array("ID"=> $pointsRow['ID'],
"PointNum"=> $pointsRow['PointNum'],
"DateRecorded"=>$pointsRow['DateRecorded'],
"XCoord" => $pointsRow['XCoord'],
"YCoord" => $pointsRow['YCoord'],
"ZCoord" => $pointsRow['ZCoord'],
"TargetDetected"=>$pointsRow['TargetDetected'],
"TargetTypeID"=>$pointsRow['TargetTypeID'],
"Annotation"=>$pointsRow['Annotation'],
"ImageURL"=>$pointsRow['ImageURL'],
"WindSpeed"=>$pointsRow['WindSpeed'],
"WindBearing"=>$pointsRow['WindBearing']);
} $missions[] = array("ID"=> $row['ID'],
"LocationID"=> $row['LocationID'],
"DateFlown" => $row['DateFlown'],
"Duration"=>$row['Duration'],
"DistanceFlown"=>$row['DistanceFlown'],
"TargetsDetected"=>$row['TargetsDetected'],
"PointCount"=>$row['PointCount'],
"Description"=>$row['Description'],
"MissionPoints"=>$missionPoints
);
}
} } $app->response()->header("Content-Type", "application/json");
echo json_encode($missions);
}else{
$app->response()->status(401);
$app->response()->header("Content-Type", "application/json");
$resp = array();
$resp[] = array("Error"=> "LocationID does not exist");
echo json_encode($resp);
} close_connection();
}else
{
$app->response()->status(401);
$app->response()->header("Content-Type", "application/json");
$resp = array();
$resp[] = array("Error"=> "Authenticaton error for user " + $user);
echo json_encode($resp);
}
}); //add a location
$app->post("/location/:user/:password/:location/:town/:locState/:MinX/:MinY/:MaxX/:MaxY", function($user, $password, $location, $town, $locState, $MinX, $MinY, $MaxX, $MaxY) use($app)
{
$user = $app->request()->post('username');
$password = $app->request()->post('password');
$mission = $app->request()->post('mission'); $type = authenticate_user($user, $password);
if($type == "PILOT")
{
if(connect($type))
{
if(mysql_query("INSERT INTO locations (LocationName, Town, LocState, Country, MinX, MinY, MaxX, MaxY) VALUES ('$location', '$town', '$locState', 'Australia', $MinX, $MinY, $MaxX, $MaxY);"))
{ $app->response()->header("Content-Type", "application/json");
$response = array();
$response[] = array("LocationID"=>mysql_insert_id());
echo json_encode($response);
}else{
$app->response()->status(400);
$app->response()->header("Content-Type", "text/plain");
}
close_connection();
}else
{
$app->response()->status(400);
$app->response()->header("Content-Type", "text/plain");
}
}else
{
$app->response()->status(401);
$app->response()->header("Content-Type", "text/plain");
}
}); //add a mission
$app->post("/mission/new", function() use($app)
{
$user = $app->request()->post('username');
$password = $app->request()->post('password');
$mission = $app->request()->post('mission'); $type = authenticate_user($user, $password);
if($type == "PILOT")
{
if(connect($type))
{
$missionData = json_decode($mission, true);
if(count($missionData) > 0 && array_key_exists("MissionPoints", $missionData))
{
$dateFlown = convertJSONtoPHPDate($missionData[DateFlown]);
//echo($date);
if(mysql_query("INSERT INTO missions (LocationID, DateFlown, PilotID, AircraftID, CameraID, Duration, DistanceFlown, TargetsDetected,PointCount,Description,MissionVideo, MissionLog)
VALUES ($missionData[LocationID], '$dateFlown', $missionData[PilotID], $missionData[AircraftID], $missionData[CameraID], $missionData[Duration],
$missionData[DistanceFlown], $missionData[TargetsDetected], $missionData[PointCount], '$missionData[Description]','$missionData[MissionVideo]','$missionData[MissionLog]')"))
{ $missionID = mysql_insert_id(); $missionPoints = $missionData['MissionPoints'];
$ptsAdded = true;
foreach($missionPoints as $pt)
{ $targetTypeID = "null";
if($pt[TargetTypeID] > 0)
{
$targetTypeID = $pt['TargetTypeID'];
} $imageURL = "null";
$imageIndex = 0;
if(array_key_exists("ImageIndex", $pt))
{
$imageIndex = $pt['ImageIndex'];
if($imageIndex > 0 && array_key_exists("Image", $pt))
{
if($pt['Image'] != null)
{
//echo("pt index = ".$imageIndex);
$imageURL = save_image($pt['Image']);
//var_dump($imageURL);
$imageURL = "'".$imageURL."'";
}
}
} $timeStampPoint = convertJSONtoPHPDate($pt[DateRecorded]);
if(!mysql_query("INSERT INTO mission_points (MissionID, PointNum, XCoord, YCoord, ZCoord, TargetDetected, TargetTypeID, Annotation, DateRecorded, ImageIndex, ImageURL, WindSpeed, WindBearing)
VALUES ($missionID, $pt[PointNum], $pt[XCoord], $pt[YCoord], $pt[ZCoord], $pt[TargetDetected],$targetTypeID, '$pt[Annotation]', '$timeStampPoint', $imageIndex, $imageURL, $pt[WindSpeed], $pt[WindBearing])"))
{
$app->response()->status(400);
$app->response()->header("Content-Type", "text/plain");
echo("Failed to add mission point $pt[PointNum] to database");
$ptsAdded = false;
break;
} } if($ptsAdded)
{
$app->response()->header("Content-Type", "application/json");
$response = array();
$response[] = array("ID"=>$missionID);
echo json_encode($response);
}
}else{
$app->response()->status(400);
$app->response()->header("Content-Type", "text/plain");
echo("Failed to add mission to database");
}
}else{
$app->response()->status(400);
$app->response()->header("Content-Type", "text/plain");
echo("Malformed json data structure");
} close_connection();
}else{
$app->response()->status(400);
$app->response()->header("Content-Type", "text/plain");
echo("No db connection");
}
}else
{
$app->response()->status(401);
$app->response()->header("Content-Type", "text/plain");
echo("cant validate user");
}
}); $app->run();
?>

2.

https://github.com/acubeinnovations/cc_user_api

第5月第7天 php slim的更多相关文章

  1. 微软Xbox360 E与微软Xbox360 slim Kinect套装(1TB)哪个好

    原文地址:http://product.pchome.net/digi_home_playstation_microsoft_xbox360slimkinect1tb/381793.html 微软Xb ...

  2. tensorflow中slim模块api介绍

    tensorflow中slim模块api介绍 翻译 2017年08月29日 20:13:35   http://blog.csdn.net/guvcolie/article/details/77686 ...

  3. WINDOWS 同步(Interlocked,InterlockedExchangeAdd,Slim读/写锁,WaitForSingleObject,CreateWaitableTimer等等)

    NOTE0 在以下两种基本情况下,线程之间需要相互通信: 需要让多个线程同时访问一个共享资源,同时不能破坏资源的完整性: 一个线程需要通知其它线程某项任务已经完成 1.原子访问:Interlocked ...

  4. 猖獗的假新闻:2017年1月1日起iOS的APP必须使用HTTPS

    一.假新闻如此猖獗 刚才一位老同事 打电话问:我们公司还是用的HTTP,马上就到2017年了,提交AppStore会被拒绝,怎么办? 公司里已经有很多人问过这个问题,回答一下: HTTP还是可以正常提 ...

  5. js获取给定月份的N个月后的日期

    1.在讲js获取给定月份的N个月后的日期之前,小颖先给大家讲下getFullYear().getYear()的区别. ①getYear() var d = new Date() console.log ...

  6. 张小龙宣布微信小程序1月9日发布,并回答了大家最关心的8个问题

    2016 年 12 月 28 日,张小龙在微信公开课 PRO 版的会场上,宣布了微信小程序的正式发布时间. 微信小程序将于 2017 年 1 月 9 号正式上线. 同时他解释称,小程序就像PC时代的网 ...

  7. 【代码笔记】iOS-获得当前的月的天数

    一,代码. #import "ViewController.h" @interface ViewController () @end @implementation ViewCon ...

  8. 怎样两个月完成Udacity Data Analyst Nanodegree

    在迷恋数据科学很久后,我决定要在MOOC网站上拿到一份Data Science的证书.美国三个MOOC网站,Udacity上的课程已经被分成了数个nanodegree,每个nanodegree都是目前 ...

  9. 我想立刻辞职,然后闭关学习编程语言,我给自己3个月时间学习C语言!这样行的通吗

    文章背景,回答提问:我想立刻辞职,然后闭关学习编程语言,我给自己3个月时间学习C语言!这样行的通吗? 我的建议是这样:1. 不要辞职.首先说,你对整个开发没有一个简单的了解,或一个系统的入门学习.换句 ...

随机推荐

  1. CSS遮罩mask

    前面的话 CSS遮罩是2008年4月由苹果公司添加到webkit引擎中的.遮罩提供一种基于像素级别的,可以控制元素透明度的能力,类似于png24位或png32位中的alpha透明通道的效果.本文将详细 ...

  2. python 模块之-hashlib

    python 模块hashlib import hashlib m=hashlib.md5()         # 生成MD5加密对象 m.update('jiami-string'.encode(' ...

  3. BZOJ1022[SHOI2008]小约翰的游戏——anti-SG(反尼姆博弈)

    题目描述 小约翰经常和他的哥哥玩一个非常有趣的游戏:桌子上有n堆石子,小约翰和他的哥哥轮流取石子,每个人取的时候,可以随意选择一堆石子,在这堆石子中取走任意多的石子,但不能一粒石子也不取,我们规定取到 ...

  4. BZOJ2002[Hnoi2010]弹飞绵羊——LCT

    题目描述 某天,Lostmonkey发明了一种超级弹力装置,为了在他的绵羊朋友面前显摆,他邀请小绵羊一起玩个游戏.游戏一开始,Lostmonkey在地上沿着一条直线摆上n个装置,每个装置设定初始弹力系 ...

  5. BZOJ3829[Poi2014]FarmCraft——树形DP+贪心

    题目描述 In a village called Byteville, there are   houses connected with N-1 roads. For each pair of ho ...

  6. 快乐的Lambda表达式(一)

    转载:http://www.cnblogs.com/jesse2013/p/happylambda.html 原文出处: Florian Rappl   译文出处:Jesse Liu 自从Lambda ...

  7. length、length()、size()区别 List与String相互转换

      字符串 数组 List对象 定义 String str = ""; String[] s = new String[5]; char[] s; List<String&g ...

  8. 洛谷P3345 [ZJOI2015]幻想乡战略游戏(动态点分治,树的重心,二分查找,Tarjan-LCA,树上差分)

    洛谷题目传送门 动态点分治小白,光是因为思路不清晰就耗费了不知道多少时间去gang这题,所以还是来理理思路吧. 一个树\(T\)里面\(\sum\limits_{v\in T} D_vdist(u,v ...

  9. SharePoint 2013 Central Admin 不能打开

    当我准备打开CA时发现下面的错误: This operation can be performed only on a computer that is joined to a server farm ...

  10. JNative 传递参数bug

    下载JNative网址:http://sourceforge.net/projects/jnative/files/jnative/ 下载JNative版本:JNative_1.4RC3_bin.zi ...