po/src/rpc.php

209 lines
6.8 KiB
PHP

<?php
// Copyright (C) 2002-2006 Balint Kis (balint@k-i-s.net)
// Copyright (C) 2005-2013 Solomon Peachy (pizza@shaftnet.org)
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 3 of the License, or
// (at your option) any later version.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
include_once "include/config.php";
include_once "include/site.php";
include_once "include/common.php";
include_once "include/search.php";
require_once 'include/JsonRPC/Server.php';
use JsonRPC\Server;
$server = new Server;
$get_folders = function($type, $id, $offset = 0, $limit = 25, $order = 2) {
global $database;
global $po_user;
global $po_options;
global $passwords;
global $folder_order_by_string;
$sqlstr = "";
$offset = pg_escape_string($database, $offset);
$limit = pg_escape_string($database, $limit);
$order = pg_escape_string($database, $order);
$order_by = $folder_order_by_string[$order][0];
$access_rights = "can_access_folder(folder.identifier, $po_user[id], '{".$passwords."}')";
$id = pg_escape_string($database, $id);
switch($type) {
case 'user':
$sqlstr = " folder.users = '$id' and folder.parent_folder is null ";
break;
case 'folder':
$sqlstr = " folder.parent_folder = '$id' ";
break;
case 'tag':
$keywords = extract_keywords($id, $po_options['search_enable_stemming']);
$sqlstr = build_sql_search_string($keywords, "folder.caption");
$sqlstr .= " OR " . build_sql_search_string($keywords, "folder.description");
break;
default:
break;
}
$search_result = pg_query($database,
"select folder.identifier, caption, date_of_creation, folder.date_changed, folder.description,
count_subfolders_by_folder(folder.identifier, $po_user[id], '{".$passwords."}') as subs,
count_photos_by_folder(folder.identifier, $po_user[id], '{".$passwords."}') as photos, thumb_ver
from folder
where ($sqlstr)
and $access_rights
order by $order_by
offset $offset
limit $limit");
$search_result = pg_fetch_all($search_result);
return $search_result;
};
$get_albums = function($type, $id, $offset = 0, $limit = 25, $order = 2) {
global $database;
global $po_user;
global $po_options;
global $passwords;
global $folder_order_by_string;
$sqlstr = "";
$offset = pg_escape_string($database, $offset);
$limit = pg_escape_string($database, $limit);
$order = pg_escape_string($database, $order);
$order_by = $folder_order_by_string[$order][0];
$access_rights = "can_access_album(album.identifier, $po_user[id], '{".$passwords."}')";
$id = pg_escape_string($database, $id);
switch($type) {
case 'user':
$sqlstr = " album.users = '$id' and album.parent_album is null ";
break;
case 'album':
$sqlstr = " album.parent_album = '$id' ";
break;
case 'tag':
$keywords = extract_keywords($id, $po_options['search_enable_stemming']);
$sqlstr = build_sql_search_string($keywords, "album.caption");
$sqlstr .= " OR " . build_sql_search_string($keywords, "album.description");
break;
default:
break;
}
$search_result = pg_query($database,
"select album.identifier, caption, date_of_creation, album.date_changed, album.description,
count_subalbums_by_album(album.identifier, $po_user[id], '{".$passwords."}') as subs,
count_photos_by_album(album.identifier, $po_user[id], '{".$passwords."}') as photos, thumb_ver
from album
where ($sqlstr)
and $access_rights
order by $order_by
offset $offset
limit $limit");
$search_result = pg_fetch_all($search_result);
return $search_result;
};
$get_photos = function($type, $id, $offset = 0, $limit = 25, $order = 2) {
global $database;
global $po_user;
global $po_options;
global $passwords;
global $folder_order_by_string;
$sqlstr = "";
$offset = pg_escape_string($database, $offset);
$limit = pg_escape_string($database, $limit);
$order = pg_escape_string($database, $order);
$id = pg_escape_string($database, $id);
$order_by = $folder_order_by_string[$order][0];
$access_rights = "can_access_album(album.identifier, $po_user[id], '{".$passwords."}')";
switch($type) {
case 'user':
$froms = array();
$master = "and photo_version.master = 't'"; //optional?
$filter = "photo.users = '$id' $master";
break;
case 'folder':
$froms = array('folder');
$master = "and photo_version.master = 't'"; //optional?
$filter = "photo.folder = '$id'
and folder.identifier = photo.folder $master";
break;
case 'album':
$froms = array('album_content');
$filter = "photo_version.identifier = album_content.version
and photo.identifier = album_content.photo
and album_content.album = $id";
break;
case 'tag':
$keywords = extract_keywords($search_data, $po_options['search_enable_stemming']);
$sql_combined_search_string = build_sql_search_string_keywords($keywords);
$froms = array();
$master_args = "and photo_version.master = 't'";
$filter = " ($sql_combined_search_string) $master_args";
break;
default:
break;
}
// XXX figure out available sizes?
$photo_data = get_photo_query($database, array(), $froms, $filter, $offset, $limit, $order);
$photo_args = array("size" => 2);
for ($i = 0 ; $i < sizeof($photo_data); $i++) {
$photo_args = array("ver" => $photo_data[$i]['version'],
"size" => '1');
$photo_data[$i]['thumb'] = generate_link('image', $photo_data[$i]['identifier'], $photo_args);
$photo_args = array("ver" => $photo_data[$i]['version'],
"size" => '2');
$photo_data[$i]['preview'] = generate_link('image', $photo_data[$i]['identifier'], $photo_args);
}
return $photo_data;
};
$server->register('folders', $get_folders);
$server->register('albums', $get_albums);
$server->register('photos', $get_photos);
/* Do the actual work */
$database = site_prolog();
echo $server->execute();
site_epilog($database);
?>