root/trunk/plog-remote.php

Revision 588, 7.8 KB (checked in by kimparsell, 3 weeks ago)

+ Minor markup changes for install/upgrade process.
+ Minor admin section changes to make action notices consistent.
+ Miscellaneous cleanup.

Line 
1<?php
2/*
3
4Support for Gallery remote protocol, details at
5http://gallery.menalto.com/modules.php?op=modload&name=GalleryDocs&file=index&page=gallery-remote.protocol.php
6
7Written by Anti Veeranna (http://masendav.com)
8
9*/
10error_reporting(E_ALL);
11require_once(dirname(__FILE__)."/plog-load-config.php");
12include(PLOGGER_DIR."plog-admin/plog-admin-functions.php");
13
14define("DEBUG",0);
15$debug_msgs = "";
16define("GR_SERVER_VERSION","2.14");
17
18define("GR_STAT_SUCCESS",0);
19define("GR_STAT_PROTO_MAJ_VER_INVAL",101);
20define("GR_STAT_PROTO_MIN_VER_INVAL",102);
21define("GR_STAT_PROTO_VER_FMT_INVAL",103);
22define("GR_STAT_PROTO_VER_MISSING",104);
23define("GR_STAT_PASSWORD_WRONG",201);
24define("GR_STAT_LOGIN_MISSING",202);
25define("GR_STAT_UNKNOWN_CMD",301);
26define("GR_STAT_NO_ADD_PERMISSION",401);
27define("GR_STAT_NO_FILENAME",402);
28define("GR_STAT_UPLOAD_PHOTO_FAIL",403);
29define("GR_STAT_NO_WRITE_PERMISSION",404);
30define("GR_STAT_NO_CREATE_ALBUM_PERMISSION",501);
31define("GR_STAT_CREATE_ALBUM_FAILED",502);
32
33class response {
34    function response() {
35        $this->keys = array();
36        $this->keys["server_version"] = GR_SERVER_VERSION;
37    }
38
39    function set_key($key,$value) {
40        $this->keys[$key] = $value;
41    }
42
43    function write() {
44        print "#__GR2PROTO__\n";
45        foreach($this->keys as $key => $val) print "${key}=${val}\n";
46    }
47}
48
49function get_album_by_name($name) {
50    $sqlAlbum = "SELECT * FROM `".TABLE_PREFIX."albums` WHERE name = '".mysql_real_escape_string($name)."'";
51    $resultAlbum = run_query($sqlAlbum);
52    return mysql_fetch_assoc($resultAlbum);
53};
54
55function login($user,$password) {
56    global $response;
57    global $config;
58    if (($user == $config["admin_username"]) && (md5($password) == $config["admin_password"])) {
59        $response->set_key("status",GR_STAT_SUCCESS);
60        $response->set_key("status_text","Login successful");
61    } else {
62        $response->set_key("status",GR_STAT_PASSWORD_WRONG);
63        $response->set_key("status_text","Login failed");
64    };
65};
66
67function list_albums() {
68    global $config;
69
70    # on first level we show collections
71    $sqlCollections = "SELECT * FROM `".TABLE_PREFIX."collections` ORDER BY `name` ASC";
72    $resultCollections = run_query($sqlCollections);
73    $albums = $parents = array();
74    $albums[1] = array(
75    "name" => "Plogger",
76    "title" => $config["gallery_name"],
77    "summary" => "1",
78    "parent" => 0,
79    # no pictures here
80    "perms.add" => "false",
81    "perms.write" => "false",
82    "perms.del_item" => "false",
83    "perms.del_alb" => "false",
84    # but albums can be created
85    "perms.create_sub" => "true",
86    );
87    $i = 2;
88
89    while($rowCollection = mysql_fetch_assoc($resultCollections)){
90        $id = $rowCollection["id"];
91        $description = $rowCollection["description"];
92        $name = $rowCollection["name"];
93        if (empty($description))
94        {
95            $description = " ";
96        };
97        if (empty($name))
98        {
99            $name = "no name";
100        };
101
102        $albums[$i] = array(
103        //"name" => $rowCollection["name"],
104        //"name" => $rowCollection["description"],
105        "name" => $name,
106        "title" => $name,
107        "id" => $id,
108        # there is no usable summary
109        "summary" => "",
110        # collections are on the first level
111        "parent" => 1,
112        # images cannot be placed in the collections
113        "perms.add" => "false",
114        "perms.write" => "false",
115        "perms.del_item" => "false",
116        "perms.del_alb" => "false",
117        # but albums can be created
118        "perms.create_sub" => "true",
119        );
120        $parents[$id] = $i;
121        $i++;
122    };
123    $sqlAlbum = "SELECT * FROM `".TABLE_PREFIX."albums` ORDER BY `name` ASC";
124    $resultAlbum = run_query($sqlAlbum);
125    while ($rowAlbum = mysql_fetch_assoc($resultAlbum)){
126        $id = $rowAlbum["id"];
127        $parent_id = $parents[$rowAlbum["parent_id"]];
128        $albums[$i] = array(
129        "name" => $rowAlbum["name"],
130        "title" => $rowAlbum["name"],
131        "summary" => $rowAlbum["description"],
132        # albums belong to a collection
133        "parent" => $parent_id,
134        "resize_size" => 480,
135        "thumb_size" => 240,
136        # no acl system either, if the user is logged in, then she can add/change images
137        "perms.add" => "true",
138        "perms.write" => "true",
139        "perms.del_item" => "true",
140        # albums cannot be nested
141        "perms.create_sub" => "false",
142        );
143        $i++;
144    };
145    $i = 1;
146    global $response;
147    $response->set_key("status",GR_STAT_SUCCESS);
148    // galleryadd.pl looks for this exact status text, other clients do not care
149    $response->set_key("status_text","Fetch albums successful.");
150
151    foreach($albums as $id => $data){
152        unset($data["id"]);
153        foreach($data as $key => $val) {
154            $response->set_key("album.${key}.${i}",$val);
155        };
156        $i++;
157    };
158    $response->set_key("album_count",$i);
159    $response->set_key("can_create_root","no");
160};
161
162function list_images($albumname) {
163    global $response;
164    $response->set_key("status",GR_STAT_SUCCESS);
165    $response->set_key("status_text","List of images");
166
167    if (empty($albumname))
168    {
169        $albumname = "Plogger";
170    };
171
172    $albuminfo = get_album_by_name($albumname);
173    $i = 0;
174
175    if ($albuminfo) {
176        $sqlPictures = "SELECT * FROM `".TABLE_PREFIX."pictures` WHERE parent_album = " . intval($albuminfo['id']);
177        $resultAlbum = run_query($sqlPictures);
178        while ($rowAlbum = mysql_fetch_assoc($resultAlbum)){
179            $response->set_key("image.name.${i}",$rowAlbum["path"]);
180            //print "image.raw_width.0=400\n";
181            //print "image.raw_height.0=400\n";
182            //print "image.raw_filesize.0=40000\n";
183            $thumbname = "plog-content/thumbs/" . $rowAlbum['id'] . '-' . basename($rowAlbum["path"]);
184            $response->set_key("image.thumbName.${i}",$thumbname);
185            $i++;
186        };
187    };
188    $response->set_key("image_count",$i);
189    $server = "http://" . $_SERVER["SERVER_NAME"] . dirname($_SERVER["REQUEST_URI"]) . "/";
190    $response->set_key("baseurl",$server);
191}
192
193function gr_add_album($parent,$name,$description) {
194    // parent is the name of the collection
195    $query = "SELECT * FROM `".TABLE_PREFIX."collections` WHERE name = '" . mysql_real_escape_string($parent) . "'";
196    $result = run_query($query);
197
198    $row = mysql_fetch_assoc($result);
199
200    if (empty($name))
201    {
202        $name = "no name";
203    };
204
205    if (empty($description))
206    {
207        $description = "no description";
208    };
209
210    $parent_id = $row["id"];
211
212    $result = add_album($name,$description,$parent_id);
213    global $response;
214    if (0 == $result["id"]) {
215        $response->set_key("status",GR_STAT_CREATE_ALBUM_FAILED);
216        $response->set_key("status_text","Could not create album");
217
218    } else {
219        $response->set_key("status",GR_STAT_SUCCESS);
220        $response->set_key("status_text","Album created");
221    };
222
223}
224
225function add_image($album,$filename,$caption) {
226    $filedat = $_FILES["userfile"];
227    $albuminfo = get_album_by_name($album);
228    $src = $filedat["tmp_name"];
229    $result = add_picture($albuminfo["id"],$_FILES["userfile"]["tmp_name"],$_FILES["userfile"]["name"],$caption);
230
231    global $debug_msgs;
232    $debug_msgs .= print_r($result,true);
233
234    // and this is the place where I need the image data
235
236    global $response;
237    if ($result["picture_id"] === false) {
238        $response->set_key("status",GR_STAT_UPLOAD_PHOTO_FAIL);
239        $response->set_key("status_text","Add photo failed.");
240    } else {
241        $response->set_key("status",GR_STAT_SUCCESS);
242        // galleryadd.pl looks for this exact status text and fails if it doesn't find it
243        $response->set_key("status_text","Add photo successful.");
244    };
245}
246
247header("Content-type: text/plain");
248$cmd = isset($_POST["cmd"]) ? $_POST["cmd"] : "";
249
250if (DEBUG) {
251    $fd = fopen("debug.txt","a");
252    fwrite($fd,print_r($_POST,true));
253    fwrite($fd,print_r($_FILES,true));
254    fwrite($fd,print_r($debug_msgs,true));
255    fclose($fd);
256};
257
258$response = new response();
259
260switch($cmd)
261{
262    case "login":
263        login($_POST["uname"],$_POST["password"]);
264    break;
265
266    case "fetch-albums":
267        list_albums();
268    break;
269
270    case "fetch-album-images";
271        list_images($_POST["set_albumName"]);
272    break;
273
274    case "add-item":
275        add_image($_POST["set_albumName"],$_FILES["userfile"]["name"],$_POST["caption"]);
276    break;
277
278    case "new-album":
279        // there is a title field as well, but since plogger doesn't use it, we drop it
280        gr_add_album($_POST["set_albumName"],$_POST["newAlbumTitle"],$_POST["newAlbumDesc"]);
281    break;
282
283    default:
284    $response->set_key("status",GR_STAT_UNKNOWN_CMD);
285    $response->set_key("status_text","Unknown command.");
286};
287
288$response->write();
289?>
Note: See TracBrowser for help on using the browser.