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. python之input()、while、title()和upper()

    代码举例: # 小应用:问卷调查,记录下调查者名字和回答,询问是否继续. # 运用数据字典.while.input().title()和upper(). responses = {} flag = T ...

  2. BZOJ2819Nim——树链剖分+线段树+Nim游戏

    题目描述 著名游戏设计师vfleaking,最近迷上了Nim.普通的Nim游戏为:两个人进行游戏,N堆石子,每回合可以取其中某一堆的任意多个,可以取完,但不可以不取.谁不能取谁输.这个游戏是有必胜策略 ...

  3. Code First 重复外键

    原因:在一个表中,我有如下字段 表名:orderInfo 列名:companySend,companyReceiver 先展示表结构,(手打了,见谅) public class OrderInfo { ...

  4. ra (数论 , 莫比乌斯反演 , 整点统计)

    题意 求 \[\displaystyle \sum_{i=1}^{n} \sum_{j=1}^{n} [\mathrm{lcm} (i,j) > n] \pmod {10^9 + 7}\] . ...

  5. 【BZOJ2228】[ZJOI2011]礼物(单调栈)

    [BZOJ2228][ZJOI2011]礼物(单调栈) 题面 BZOJ 洛谷 题解 如果这个玩意不是一个三维立方体,而是一个二维的矩形,让你在里面找一个最大正方形,那么全世界都会做. 丢到三维上?似乎 ...

  6. 洛谷 P1337 [JSOI2004]平衡点 / 吊打XXX 解题报告

    P1337 [JSOI2004]平衡点 / 吊打XXX 题目描述 有 \(n\) 个重物,每个重物系在一条足够长的绳子上.每条绳子自上而下穿过桌面上的洞,然后系在一起.\(X\)处就是公共的绳结.假设 ...

  7. babel与ES6环境的搭建

    我们知道浏览器环境下直接运行ES6是存在一些兼容性问题的.那么把ES6变成ES5不就行了吗? 那如何将ES6转换成ES5呢?我们来搭建它的转换环境吧~ 第一步:初始化项目,建立写注意事项的README ...

  8. java代码示例(3)

    /** * 需求分析:根据输入的天数是否是周六或是周日, * 并且天气的温度大于28摄氏度,则外出游泳,否则钓鱼 * @author chenyanlong * 日期:2017/10/14 */ pa ...

  9. 部署python django程序

    在一台新的服务器上x需要先安装python3  ,git , 等 安装python3 安装python3 之前博客写过 创建虚拟环境,我用的是venv https://docs.python.org/ ...

  10. Nginx插件之openresty反向代理和日志滚动配置案例

    Nginx插件之openresty反向代理和日志滚动配置案例 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.openresty介绍 1>.Nginx介绍 Nginx是一款 ...