Skip to content

Commit ea0a394

Browse files
committed
CORS headers for auth
2 parents 42e5f8a + bfebcb4 commit ea0a394

File tree

5 files changed

+48
-38
lines changed

5 files changed

+48
-38
lines changed

api.php

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -705,7 +705,7 @@ public function convertFilter($field, $comparator, $value) {
705705
case 'nsis': return array('!.STIsSimple()=0',$field);
706706
case 'nsiv': return array('!.STIsValid()=0',$field);
707707
}
708-
}
708+
}
709709
return false;
710710
}
711711

@@ -1154,7 +1154,7 @@ protected function processOrderingsParameter($orderings) {
11541154
protected function convertFilter($field, $comparator, $value) {
11551155
$result = $this->db->convertFilter($field,$comparator,$value);
11561156
if ($result) return $result;
1157-
// default behavior
1157+
// default behavior
11581158
$comparator = strtolower($comparator);
11591159
if ($comparator[0]!='n') {
11601160
if (strlen($comparator)==2) {
@@ -1167,8 +1167,10 @@ protected function convertFilter($field, $comparator, $value) {
11671167
case 'le': return array('! <= ?',$field,$value);
11681168
case 'ge': return array('! >= ?',$field,$value);
11691169
case 'gt': return array('! > ?',$field,$value);
1170-
case 'bt': $v = explode(',',$value); if (count($v)<2) return false;
1171-
return array('! BETWEEN ? AND ?',$field,$v[0],$v[1]);
1170+
case 'bt':
1171+
$v = explode(',',$value);
1172+
if (count($v)<2) return false;
1173+
return array('! BETWEEN ? AND ?',$field,$v[0],$v[1]);
11721174
case 'in': return array('! IN ?',$field,explode(',',$value));
11731175
case 'is': return array('! IS NULL',$field);
11741176
}
@@ -1204,8 +1206,10 @@ protected function convertFilter($field, $comparator, $value) {
12041206
case 'nle': return array('! > ?',$field,$value);
12051207
case 'nge': return array('! < ?',$field,$value);
12061208
case 'ngt': return array('! <= ?',$field,$value);
1207-
case 'nbt': $v = explode(',',$value); if (count($v)<2) return false;
1208-
return array('! NOT BETWEEN ? AND ?',$field,$v[0],$v[1]);
1209+
case 'nbt':
1210+
$v = explode(',',$value);
1211+
if (count($v)<2) return false;
1212+
return array('! NOT BETWEEN ? AND ?',$field,$v[0],$v[1]);
12091213
case 'nin': return array('! NOT IN ?',$field,explode(',',$value));
12101214
case 'nis': return array('! IS NOT NULL',$field);
12111215
}
@@ -1518,7 +1522,7 @@ protected function addRelationColumns($columns,$select) {
15181522
foreach ($keys as $key=>$other) {
15191523
$columns.=",$table.$key,".implode('.',$other);
15201524
}
1521-
}
1525+
}
15221526
}
15231527
return $columns;
15241528
}
@@ -1896,7 +1900,7 @@ public function __construct($config) {
18961900
$get = isset($get)?$get:null;
18971901
$post = isset($post)?$post:null;
18981902
$origin = isset($origin)?$origin:null;
1899-
1903+
19001904
// defaults
19011905
if (!$dbengine) {
19021906
$dbengine = 'MySQL';
@@ -1923,7 +1927,7 @@ public function __construct($config) {
19231927
// connect
19241928
$request = trim($request,'/');
19251929
if (!$database) {
1926-
$database = $this->parseRequestParameter($request, 'a-zA-Z0-9\-_');
1930+
$database = $this->parseRequestParameter($request, 'a-zA-Z0-9\-_');
19271931
}
19281932
if (!$db) {
19291933
$db = new $dbengine();
@@ -2013,7 +2017,7 @@ protected function swagger($settings) {
20132017
$table_list = array($table['name']);
20142018
$table_fields = $this->findFields($table_list,false,$database);
20152019
$table_names = array_map(function($v){ return $v['name'];},$tables);
2016-
2020+
20172021
if ($extensions) {
20182022
$result = $this->db->query($this->db->getSql('reflect_belongs_to'),array($table_list[0],$table_names,$database,$database));
20192023
while ($row = $this->db->fetchRow($result)) {
@@ -2028,7 +2032,7 @@ protected function swagger($settings) {
20282032
$table_fields[$table['name']][$primaryKey]->primaryKey = true;
20292033
}
20302034
}
2031-
2035+
20322036
foreach (array('root_actions','id_actions') as $path) {
20332037
foreach ($table[$path] as $i=>$action) {
20342038
$table_list = array($table['name']);

examples/client.html

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44
<script>
55
var xhttp = new XMLHttpRequest();
66
xhttp.onreadystatechange = function() {
7-
if (this.readyState == 4 && this.status == 200) {
8-
console.log(this.responseText);
9-
jsonObject = php_crud_api_transform(JSON.parse(this.responseText));
10-
document.getElementById('output').innerHTML = JSON.stringify(jsonObject, undefined, 4);
11-
}
7+
if (this.readyState == 4 && this.status == 200) {
8+
console.log(this.responseText);
9+
jsonObject = php_crud_api_transform(JSON.parse(this.responseText));
10+
document.getElementById('output').innerHTML = JSON.stringify(jsonObject, undefined, 4);
11+
}
1212
};
1313
xhttp.open("GET", "http://localhost/api.php/posts?include=categories,tags,comments&filter=id,eq,1", true);
1414
xhttp.send();
@@ -18,3 +18,4 @@
1818
<pre id="output"></pre>
1919
</body>
2020
</html>
21+

examples/client.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@ function call($method, $url, $data = false) {
1616
return curl_exec($ch);
1717
}
1818

19-
$response = call('GET','http://localhost/api.php/posts?include=categories,tags,comments&filter=id,eq,1');
20-
$jsonObject = json_decode($response,true);
19+
$response = call('GET', 'http://localhost/api.php/posts?include=categories,tags,comments&filter=id,eq,1');
20+
$jsonObject = json_decode($response, true);
2121

2222
$jsonObject = php_crud_api_transform($jsonObject);
23-
$output = json_encode($jsonObject,JSON_PRETTY_PRINT);
23+
$output = json_encode($jsonObject, JSON_PRETTY_PRINT);
2424
?>
2525
<html>
2626
<head>
@@ -29,3 +29,4 @@ function call($method, $url, $data = false) {
2929
<pre><?php echo $output ?></pre>
3030
</body>
3131
</html>
32+

examples/client_angular.html

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,21 @@
55
<script>
66
var app = angular.module('myApplication', []);
77
app.controller('postController', function($scope, $http) {
8-
var url = '../api.php/posts';
9-
$http.post(url,{user_id:1,category_id:1,content:"from angular"}).success(function(){
10-
$http.get(url).success(function(response){
11-
$scope.posts = php_crud_api_transform(response).posts;
12-
});
13-
});
8+
var url = '../api.php/posts';
9+
$http.post(url, {user_id: 1, category_id: 1, content: "from angular"}).success(function() {
10+
$http.get(url).success(function(response) {
11+
$scope.posts = php_crud_api_transform(response).posts;
12+
});
13+
});
1414
});
1515
</script>
1616
</head>
1717
<body>
1818
<div ng-app="myApplication" ng-controller="postController">
19-
<ul>
20-
<li ng-repeat="x in posts">{{ x.id + ', ' + x.content }}</li>
21-
</ul>
19+
<ul>
20+
<li ng-repeat="x in posts">{{ x.id + ', ' + x.content }}</li>
21+
</ul>
2222
</div>
2323
</body>
2424
</html>
25+

examples/client_zepto.html

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,22 +24,22 @@
2424
var li = $(this).parent('li');
2525
var id = li.find('span.id').text();
2626
var content = li.find('span.content').text();
27-
content = prompt('Value',content);
27+
content = prompt('Value', content);
2828
if (content!==null) {
29-
$.ajax({url:url+'/'+id, type: 'PUT', data: {content:content}, success:self.update});
29+
$.ajax({url: url + '/' + id, type: 'PUT', data: {content: content}, success: self.update});
3030
}
3131
};
3232
self.delete = function() {
3333
var li = $(this).parent('li');
3434
var id = li.find('span.id').text();
35-
if (confirm("Deleting #"+id+". Continue?")) {
36-
$.ajax({url:url+'/'+id, type: 'DELETE', success:self.update});
35+
if (confirm("Deleting #" + id + ". Continue?")) {
36+
$.ajax({url: url + '/' + id, type: 'DELETE', success: self.update});
3737
}
3838
};
3939
self.submit = function(e) {
4040
e.preventDefault();
4141
var content = $(this).find('input[name="content"]').val();
42-
$.post(url, {user_id:1,category_id:1,content:content}, self.update);
42+
$.post(url, {user_id: 1, category_id: 1, content: content}, self.update);
4343
};
4444
self.render = function(data) {
4545
data = php_crud_api_transform(data);
@@ -58,17 +58,20 @@
5858
$.get(url, self.render);
5959
};
6060
self.post = function() {
61-
$.post(url, {user_id:1,category_id:1,content:"from zepto"}, self.update);
61+
$.post(url, {user_id: 1, category_id: 1, content: "from zepto"}, self.update);
6262
};
63-
element.on('submit','form',self.submit);
64-
element.on('click','a.edit',self.edit)
65-
element.on('click','a.delete',self.delete)
63+
element.on('submit', 'form', self.submit);
64+
element.on('click', 'a.edit', self.edit);
65+
element.on('click', 'a.delete', self.delete);
6666
self.post();
6767
};
68-
$(function(){ new PostList($('#PostListDiv'),$('#PostListTemplate')); });
68+
$(function(){
69+
new PostList($('#PostListDiv'), $('#PostListTemplate'));
70+
});
6971
</script>
7072
</head>
7173
<body>
7274
<div id="PostListDiv">Loading...</div>
7375
</body>
7476
</html>
77+

0 commit comments

Comments
 (0)