ploto

ploto Git Source Tree


Root/plog-remote.php

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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
<?php
/*
Support for Gallery remote protocol, details at
 
Written by Anti Veeranna (http://masendav.com)
*/
 
error_reporting(E_ALL);
 
require_once(dirname(__FILE__).'/plog-load-config.php');
include_once(PLOGGER_DIR.'plog-admin/plog-admin-functions.php');
 
define('DEBUG', 0);
$debug_msgs = '';
define('GR_SERVER_VERSION', '2.14');
 
define('GR_STAT_SUCCESS', 0);
define('GR_STAT_PROTO_MAJ_VER_INVAL', 101);
define('GR_STAT_PROTO_MIN_VER_INVAL', 102);
define('GR_STAT_PROTO_VER_FMT_INVAL', 103);
define('GR_STAT_PROTO_VER_MISSING', 104);
define('GR_STAT_PASSWORD_WRONG', 201);
define('GR_STAT_LOGIN_MISSING', 202);
define('GR_STAT_UNKNOWN_CMD', 301);
define('GR_STAT_NO_ADD_PERMISSION', 401);
define('GR_STAT_NO_FILENAME', 402);
define('GR_STAT_UPLOAD_PHOTO_FAIL', 403);
define('GR_STAT_NO_WRITE_PERMISSION', 404);
define('GR_STAT_NO_CREATE_ALBUM_PERMISSION', 501);
define('GR_STAT_CREATE_ALBUM_FAILED', 502);
 
class response {
    function response() {
        $this->keys = array();
        $this->keys['server_version'] = GR_SERVER_VERSION;
    }
 
    function set_key($key, $value) {
        $this->keys[$key] = $value;
    }
 
    function write() {
        print "#__GR2PROTO__\n";
        foreach($this->keys as $key => $val) print "${key}=${val}\n";
    }
}
 
function get_album_by_name($name) {
    $sqlAlbum = "SELECT * FROM `".PLOGGER_TABLE_PREFIX."albums` WHERE name = '".mysql_real_escape_string($name)."'";
    $resultAlbum = run_query($sqlAlbum);
    return mysql_fetch_assoc($resultAlbum);
}
 
function login($user, $password) {
    global $response;
    global $config;
 
    if (($user == $config['admin_username']) && (md5($password) == $config['admin_password'])) {
        $response->set_key('status', GR_STAT_SUCCESS);
        $response->set_key('status_text', 'Login successful');
    } else {
        $response->set_key('status', GR_STAT_PASSWORD_WRONG);
        $response->set_key('status_text', 'Login failed');
    }
}
 
function list_albums() {
    global $config;
 
    // On first level we show collections
    $sqlCollections = "SELECT * FROM `".PLOGGER_TABLE_PREFIX."collections` ORDER BY `name` ASC";
    $resultCollections = run_query($sqlCollections);
    $albums = $parents = array();
    $albums[1] = array(
    'name' => 'Plogger',
    'title' => $config['gallery_name'],
    'summary' => '1',
    'parent' => 0,
    // No pictures here
    'perms.add' => 'false',
    'perms.write' => 'false',
    'perms.del_item' => 'false',
    'perms.del_alb' => 'false',
    // But albums can be created
    'perms.create_sub' => 'true',
    );
    $i = 2;
 
    while($rowCollection = mysql_fetch_assoc($resultCollections)) {
        $id = $rowCollection['id'];
        $description = $rowCollection['description'];
        $name = $rowCollection['name'];
        if (empty($description)) {
            $description = ' ';
        }
        if (empty($name)) {
            $name = 'no name';
        }
 
        $albums[$i] = array(
        //'name' => $rowCollection['name'],
        //'name' => $rowCollection['description'],
        'name' => $name,
        'title' => $name,
        'id' => $id,
        // There is no usable summary
        'summary' => '',
        // Collections are on the first level
        'parent' => 1,
        // Images cannot be placed in the collections
        'perms.add' => 'false',
        'perms.write' => 'false',
        'perms.del_item' => 'false',
        'perms.del_alb' => 'false',
        // But albums can be created
        'perms.create_sub' => 'true',
        );
        $parents[$id] = $i;
        $i++;
    }
 
    $sqlAlbum = "SELECT * FROM `".PLOGGER_TABLE_PREFIX."albums` ORDER BY `name` ASC";
    $resultAlbum = run_query($sqlAlbum);
    while ($rowAlbum = mysql_fetch_assoc($resultAlbum)) {
        $id = $rowAlbum['id'];
        $parent_id = $parents[$rowAlbum['parent_id']];
        $albums[$i] = array(
        'name' => $rowAlbum['name'],
        'title' => $rowAlbum['name'],
        'summary' => $rowAlbum['description'],
        // Albums belong to a collection
        'parent' => $parent_id,
        'resize_size' => 480,
        'thumb_size' => 240,
        // No acl system either, if the user is logged in, then they can add/change images
        'perms.add' => 'true',
        'perms.write' => 'true',
        'perms.del_item' => 'true',
        // Albums cannot be nested
        'perms.create_sub' => 'false',
        );
        $i++;
    }
 
    $i = 1;
 
    global $response;
 
    $response->set_key('status', GR_STAT_SUCCESS);
    // galleryadd.pl looks for this exact status text, other clients do not care
    $response->set_key('status_text', 'Fetch albums successful.');
 
    foreach($albums as $id => $data) {
        unset($data['id']);
        foreach($data as $key => $val) {
            $response->set_key("album.${key}.${i}", $val);
        }
        $i++;
    }
    $response->set_key('album_count', $i);
    $response->set_key('can_create_root', 'no');
}
 
function list_images($albumname) {
    global $response;
    $response->set_key('status', GR_STAT_SUCCESS);
    $response->set_key('status_text', 'List of images');
 
    if (empty($albumname)) {
        $albumname = 'Plogger';
    }
 
    $albuminfo = get_album_by_name($albumname);
    $i = 0;
 
    if ($albuminfo) {
        $sqlPictures = "SELECT * FROM `".PLOGGER_TABLE_PREFIX."pictures` WHERE parent_album = ".intval($albuminfo['id']);
        $resultAlbum = run_query($sqlPictures);
        while ($rowAlbum = mysql_fetch_assoc($resultAlbum)) {
            $response->set_key("image.name.${i}", $rowAlbum['path']);
            //print "image.raw_width.0=400\n";
            //print "image.raw_height.0=400\n";
            //print "image.raw_filesize.0=40000\n";
            $thumbname = 'plog-content/thumbs/'.$rowAlbum['id'].'-'.basename($rowAlbum['path']);
            $response->set_key("image.thumbName.${i}", $thumbname);
            $i++;
        }
    }
 
    $response->set_key('image_count', $i);
    $server = 'http://'.$_SERVER['SERVER_NAME'].dirname($_SERVER['REQUEST_URI']).'/';
    $response->set_key('baseurl', $server);
}
 
function gr_add_album($parent, $name, $description) {
    // Parent is the name of the collection
    $query = "SELECT * FROM `".PLOGGER_TABLE_PREFIX."collections` WHERE name = '".mysql_real_escape_string($parent)."'";
    $result = run_query($query);
 
    $row = mysql_fetch_assoc($result);
 
    if (empty($name)) {
        $name = 'no name';
    }
 
    if (empty($description)) {
        $description = 'no description';
    }
 
    $parent_id = $row['id'];
 
    $result = add_album($name, $description, $parent_id);
 
    global $response;
 
    if (0 == $result['id']) {
        $response->set_key('status', GR_STAT_CREATE_ALBUM_FAILED);
        $response->set_key('status_text', 'Could not create album');
    } else {
        $response->set_key('status', GR_STAT_SUCCESS);
        $response->set_key('status_text', 'Album created');
    }
}
 
function add_image($album, $filename, $caption) {
    $filedat = $_FILES['userfile'];
    $albuminfo = get_album_by_name($album);
    $src = $filedat['tmp_name'];
    $result = add_picture($albuminfo['id'], $_FILES['userfile']['tmp_name'], $_FILES['userfile']['name'], $caption);
 
    global $debug_msgs;
    $debug_msgs .= print_r($result, true);
 
    // And this is the place where I need the image data
 
    global $response;
    if ($result['picture_id'] === false) {
        $response->set_key('status', GR_STAT_UPLOAD_PHOTO_FAIL);
        $response->set_key('status_text', 'Add photo failed.');
    } else {
        $response->set_key('status', GR_STAT_SUCCESS);
        // galleryadd.pl looks for this exact status text and fails if it doesn't find it
        $response->set_key('status_text', 'Add photo successful.');
    }
}
 
header('Content-type: text/plain');
$cmd = isset($_POST['cmd']) ? $_POST['cmd'] : '';
 
if (DEBUG) {
    $fd = fopen('debug.txt', 'a');
    fwrite($fd, print_r($_POST, true));
    fwrite($fd, print_r($_FILES, true));
    fwrite($fd, print_r($debug_msgs, true));
    fclose($fd);
}
 
$response = new response();
 
switch($cmd) {
    case 'login':
        login($_POST['uname'], $_POST['password']);
    break;
 
    case 'fetch-albums':
        list_albums();
    break;
 
    case 'fetch-album-images';
        list_images($_POST['set_albumName']);
    break;
 
    case 'add-item':
        add_image($_POST['set_albumName'], $_FILES['userfile']['name'], $_POST['caption']);
    break;
 
    case 'new-album':
        // There is a title field as well, but since Plogger doesn't use it, we drop it
        gr_add_album($_POST['set_albumName'], $_POST['newAlbumTitle'], $_POST['newAlbumDesc']);
    break;
 
    default:
    $response->set_key('status', GR_STAT_UNKNOWN_CMD);
    $response->set_key('status_text', 'Unknown command.');
}
 
$response->write();
close_db();
 
?>

Archive Download this file

Branches

Number of commits:
Page rendered in 0.08373s using 11 queries.