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. Lodop图片输出ADD_PRINT_IMAGE 有白边

    ADD_PRINT_IMAGE输出图片,如果使用img标签(即超文本<img标签),是超文本,无论是相对路径,网络图片,还是base64,都可能有白边,这可能和超文本解析有关.ADD_PRINT ...

  2. STM32外设地址查询

    问题的提出 DMA传输SDIO驱动的SD卡的数据,其中外设地址的确定 问题的解决 打开数据参考手册,在存储器和总线架构一章存储器映像小节,有一个寄存器组起始地址表,列举所有外设对应的起始地址,再到相应 ...

  3. Spring MVC 起步

    跟踪Spring MVC的请求 在请求离开浏览器时①,会带有用户所请求内容的信息,至少会包含请求的URL. 请求旅程的第一站是Spring的DispatcherServlet.与大多数基于Java的W ...

  4. websocket c++ example

    //============================================================================ // Name : websocket.c ...

  5. Luogu 1080 【NOIP2012】国王游戏 (贪心,高精度)

    Luogu 1080 [NOIP2012]国王游戏 (贪心,高精度) Description 恰逢H国国庆,国王邀请n位大臣来玩一个有奖游戏.首先,他让每个大臣在左.右手上面分别写下一个整数,国王自己 ...

  6. A1012. The Best Rank

    To evaluate the performance of our first year CS majored students, we consider their grades of three ...

  7. 【洛谷P3586】LOG

    题目大意:维护一个集合,支持单点修改.查询小于 X 的数的个数.查询小于 X 的数的和. 题解:学习到了动态开点线段树.对于一棵未经离散化的权值线段树来说,对于静态开点来说,过大的值域会导致不能承受的 ...

  8. 查询字符串(性能对比): Array Vs HashMap

    ip字符串长度: 15 ip count: 25 time - array:16ms, 查询次数:25000time - map:15ms, 查询次数:25000 ip count: 42 time ...

  9. hinton教授的本科生课程CSC321-机器学习中的神经网的笔记

    最近一直在看仙守博友所记录的笔记 Hinton的CSC321课程(完结,待文字润色): 1.lecture1-NN的简介 2.lecture2-NN结构的主要类型的概述和感知机 3.lecture3- ...

  10. (大数 string) Integer Inquiry hdu1047

    Integer Inquiry Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...