第5月第7天 php slim
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的更多相关文章
- 微软Xbox360 E与微软Xbox360 slim Kinect套装(1TB)哪个好
原文地址:http://product.pchome.net/digi_home_playstation_microsoft_xbox360slimkinect1tb/381793.html 微软Xb ...
- tensorflow中slim模块api介绍
tensorflow中slim模块api介绍 翻译 2017年08月29日 20:13:35 http://blog.csdn.net/guvcolie/article/details/77686 ...
- WINDOWS 同步(Interlocked,InterlockedExchangeAdd,Slim读/写锁,WaitForSingleObject,CreateWaitableTimer等等)
NOTE0 在以下两种基本情况下,线程之间需要相互通信: 需要让多个线程同时访问一个共享资源,同时不能破坏资源的完整性: 一个线程需要通知其它线程某项任务已经完成 1.原子访问:Interlocked ...
- 猖獗的假新闻:2017年1月1日起iOS的APP必须使用HTTPS
一.假新闻如此猖獗 刚才一位老同事 打电话问:我们公司还是用的HTTP,马上就到2017年了,提交AppStore会被拒绝,怎么办? 公司里已经有很多人问过这个问题,回答一下: HTTP还是可以正常提 ...
- js获取给定月份的N个月后的日期
1.在讲js获取给定月份的N个月后的日期之前,小颖先给大家讲下getFullYear().getYear()的区别. ①getYear() var d = new Date() console.log ...
- 张小龙宣布微信小程序1月9日发布,并回答了大家最关心的8个问题
2016 年 12 月 28 日,张小龙在微信公开课 PRO 版的会场上,宣布了微信小程序的正式发布时间. 微信小程序将于 2017 年 1 月 9 号正式上线. 同时他解释称,小程序就像PC时代的网 ...
- 【代码笔记】iOS-获得当前的月的天数
一,代码. #import "ViewController.h" @interface ViewController () @end @implementation ViewCon ...
- 怎样两个月完成Udacity Data Analyst Nanodegree
在迷恋数据科学很久后,我决定要在MOOC网站上拿到一份Data Science的证书.美国三个MOOC网站,Udacity上的课程已经被分成了数个nanodegree,每个nanodegree都是目前 ...
- 我想立刻辞职,然后闭关学习编程语言,我给自己3个月时间学习C语言!这样行的通吗
文章背景,回答提问:我想立刻辞职,然后闭关学习编程语言,我给自己3个月时间学习C语言!这样行的通吗? 我的建议是这样:1. 不要辞职.首先说,你对整个开发没有一个简单的了解,或一个系统的入门学习.换句 ...
随机推荐
- html 佈局
html常見佈局方式有以下幾種: 1.使用div的html 利用div可以為html實現多列佈局. 2.使用html5的網站佈局, 利用html新增的header.footer.nav.section ...
- windows常见数据类型
一,常见数据类型 WORD: 16位无符号整形数据 DWORD: 32位无符号整型数据(DWORD32) DWORD64: 64位 ...
- POJ3273-Monthly Expense-二分答案
FJ对以后的每一天会花mi块钱,他想把这些天分成M个时段,然后每个时段的花费和最小. 二分答案,如果加上这天还没有达到mid,就加上它.之后看分成的时段是否大于M #include <cstdi ...
- EF 更新 删除
为了避免先查询后更新或删除的问题 可以使用如下语句 Entities db = new Entities(); Orders o = new Orders(); o.id = 6; o.name = ...
- Dependency Walker使用说明[转]
在Windows世界中,有无数块活动的大陆,它们都有一个共同的名字——动态链接库.现在就让我们走进这些神奇的活动大陆,找出它们隐藏已久的秘密吧! 初窥门径:Windows的基石 随便打开一个系统目录, ...
- 自学工业控制网络之路1.5-典型的现场总线介绍DeviceNet
返回 自学工业控制网络之路 自学工业控制网络之路1.5-典型的现场总线介绍DeviceNet 2002年10月DeviceNet被批准为中国国家标准GB/T18858.3-2002,并于2003.4. ...
- 简易版AC自动机
为什么说是简易版? 因为复杂度大概是\(O(M*\overline N)\),而似乎还有另一种大概是\(O(M+\sum N)\)的. 不过据说比赛不会卡前一种做法,因为模式串一般不会很长. 那么步入 ...
- 变量[^_^][T_T]
变量[^_^][T_T]source .bashrcget_ps1(){if [ "$?" = "0" ]then#we're on the system co ...
- [系统]安装fedora 19
再也没有什么大道至简了. ==== 步骤如下: 1. 备份. 2. 刻镜像. 选fedora-kde,gnome呵呵. 3. 分区,格式化,安装. 上面3步没什么好说的,按照官网installatio ...
- bzoj4817/luogu3703 树点涂色 (LCT+dfs序+线段树)
我们发现,这个染色的操作他就很像LCT中access的操作(为什么??),然后就自然而然地想到,其实一个某条路径上的颜色数量,就是我们做一个只有access操作的LCT,这条路径经过的splay的数量 ...