forked from germanysbestkeptsecret/Wookmark-jQuery
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.php
More file actions
184 lines (169 loc) · 4.61 KB
/
server.php
File metadata and controls
184 lines (169 loc) · 4.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
<?php
/**
* Sample php server script for a wookmark integration
*
* @author Sebastian Helzle <sebastian@helzle.net>
*/
/**
* Basic class which provides all functions to retrieve and paginate pictures
*/
class PictureDatabase {
/**
* @var array $data
*/
protected $data;
/**
* @var int $itemsPerPage
*/
protected $itemsPerPage;
function __construct($data, $itemsPerPage) {
$this->data = $data;
$this->itemsPerPage = $itemsPerPage;
}
/**
* Returns the pictures of the given page or an empty array if page doesn't exist
* @param int $page
* @return array
*/
public function getPage($page=1) {
if ($page > 0 && $page <= $this->getNumberOfPages()) {
$startOffset = ($page - 1) * $this->itemsPerPage;
return array_slice($this->data, $startOffset, $this->itemsPerPage);
}
return array();
}
/**
* Returns the maximum number of pages
* @return int
*/
public function getNumberOfPages() {
return ceil(count($this->data) / $this->itemsPerPage);
}
}
// Our data source
$data = array(
array(
'id' => "1",
'title' => "First image",
'url' => "http://www.example.org/1",
'width' => "200",
'height' => "283",
'image' => "../sample-images/image_1_big.jpg",
'preview' => "../sample-images/image_1.jpg"
),
array(
'id' => "2",
'title' => "Second image",
'url' => "http://www.example.org/2",
'width' => "200",
'height' => "300",
'image' => "../sample-images/image_2_big.jpg",
'preview' => "../sample-images/image_2.jpg"
),
array(
'id' => "3",
'title' => "Third image",
'url' => "http://www.example.org/3",
'width' => "200",
'height' => "252",
'image' => "../sample-images/image_3_big.jpg",
'preview' => "../sample-images/image_3.jpg"
),
array(
'id' => "4",
'title' => "Fourth image",
'url' => "http://www.example.org/4",
'width' => "200",
'height' => "158",
'image' => "../sample-images/image_4_big.jpg",
'preview' => "../sample-images/image_4.jpg"
),
array(
'id' => "5",
'title' => "Fifth image",
'url' => "http://www.example.org/5",
'width' => "200",
'height' => "300",
'image' => "../sample-images/image_5_big.jpg",
'preview' => "../sample-images/image_5.jpg"
),
array(
'id' => "6",
'title' => "Sixth image",
'url' => "http://www.example.org/6",
'width' => "200",
'height' => "297",
'image' => "../sample-images/image_6_big.jpg",
'preview' => "../sample-images/image_6.jpg"
),
array(
'id' => "7",
'title' => "Seventh image",
'url' => "http://www.example.org/7",
'width' => "200",
'height' => "200",
'image' => "../sample-images/image_7_big.jpg",
'preview' => "../sample-images/image_7.jpg"
),
array(
'id' => "8",
'title' => "Eight image",
'url' => "http://www.example.org/8",
'width' => "200",
'height' => "200",
'image' => "../sample-images/image_8_big.jpg",
'preview' => "../sample-images/image_8.jpg"
),
array(
'id' => "9",
'title' => "Ninth image",
'url' => "http://www.example.org/9",
'width' => "200",
'height' => "398",
'image' => "../sample-images/image_9_big.jpg",
'preview' => "../sample-images/image_9.jpg"
),
array(
'id' => "10",
'title' => "Tenth image",
'url' => "http://www.example.org/10",
'width' => "200",
'height' => "267",
'image' => "../sample-images/image_10_big.jpg",
'preview' => "../sample-images/image_10.jpg"
)
);
// Make data array a bit bigger to have more pages
for ($i=0; $i<3; $i++) {
$data = array_merge($data, $data);
}
// Create instance of picture database with 10 items per page and our data as source
$pictureDatabase = new PictureDatabase($data, 10);
$result = array(
'success' => TRUE,
'message' => 'Retrieved pictures',
'data' => array()
);
$callback = isset($_REQUEST['callback']) ? $_REQUEST['callback'] : false;
// Get requested page number from request and return error message if parameter is not a number
$page = 1;
try {
$page = intval($_REQUEST['page']);
} catch (Exception $e) {
$result['success'] = FALSE;
$result['message'] = 'Parameter page is not a number';
}
// Get data from database
$result['data'] = $pictureDatabase->getPage($page);
if (count($result['data']) == 0 || $page >= $pictureDatabase->getNumberOfPages()) {
$result['success'] = TRUE;
$result['message'] = 'No more pictures';
}
// Encode data as json or jsonp and return it
if ($callback) {
header('Content-Type: application/javascript');
echo $callback.'('.json_encode($result).')';
} else {
header('Content-Type: application/json');
echo json_encode($result);
}