forked from moy/piwigo-random
-
Notifications
You must be signed in to change notification settings - Fork 0
/
piwigo-random-backend.php
152 lines (138 loc) · 5.27 KB
/
piwigo-random-backend.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
<?php // -*- mode:php c-basic-offset: 2 -*-
// +-----------------------------------------------------------------------+
// | Piwigo RANDOM - Insert images from a Piwigo Galery in a website |
// +-----------------------------------------------------------------------+
// | Copyright(C) 2014, 2015 Matthieu Moy |
// +-----------------------------------------------------------------------+
// | 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 2 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
// | USA. |
// +-----------------------------------------------------------------------+
// Your piwigo gallery here:
$protocol = ((!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off') || $_SERVER['SERVER_PORT'] == 443 || $_SERVER['HTTP_X_FORWARDED_PORT'] == 443) ? "https://" : "http://";
$site = $protocol . "mmoy.piwigo.com/";
// You should not have to modify anything below:
$maximages = 1;
$cat_id = null;
$element_name = 'random_image';
$mode = 'javascript';
$target = '_blank';
$size = 'thumb';
$error = '';
if (is_numeric($_GET['maximages']))
{
$maximages = intval($_GET['maximages']);
}
if (is_numeric($_GET['cat_id']))
{
$cat_id = intval($_GET['cat_id']);
}
if (isset($_GET['element_name']))
{
$element_name = $_GET['element_name'];
}
if (isset($_GET['target']))
{
$target = $_GET['target'];
}
if (isset($_GET['size']))
{
$size = $_GET['size'];
}
if (isset($_GET['mode'])) {
$mode = $_GET['mode'];
}
function check_param($param, $values) {
global $error;
global $$param;
if (!in_array($$param, $values, true)) {
if ($error != '') {
$error .= ', ';
}
$error .= 'Invalid value \'' . $$param . '\' for parameter \'' . $param
. '\' (possible values are: ' . implode(', ', $values) . ')';
}
}
check_param('size', array('square', 'thumb', '2small', 'xsmall', 'small', 'medium', 'large', 'xlarge', 'xxlarge'));
check_param('mode', array('html', 'javascript'));
header('Content-Type: text/javascript');
if ($error != '') {
echo 'document.getElementById('. json_encode($element_name) . ').innerHTML = ' . json_encode(
'<strong>piwigo-random error:</strong> ' . htmlspecialchars($error) . '.') . ';';
echo 'console.log('. json_encode('piwigo-random error:' . $error) . ');';
exit(1);
}
$url = $site . "ws.php" .
"?format=php" .
"&method=pwg.categories.getImages" .
($cat_id ? "&cat_id=" . $cat_id : "") .
"&recursive=true" .
"&per_page=" . $maximages .
"&page=1" .
"&order=random";
$response = file_get_contents($url);
$thumbc = unserialize($response);
if ($thumbc["stat"] === 'ok')
{
foreach ($thumbc["result"]["images"] as $image)
{
$image_url = (string)$image['derivatives'][$size]['url'];
$page_url = (string)$image['page_url'];
$comment = (string)$image['comment'];
if ($comment === '') {
$comment = "Random image";
}
$comment .= "\n(click for full-size)";
if ($mode === 'javascript') {
// Would be a bit simpler with jquery, but let's not
// force it for such a simple piece of code.
?>
var newImg = document.createElement("img");
newImg.src = <?php echo json_encode($image_url); ?>;
newImg.alt = "";
newImg.title = <?php echo json_encode($comment) ?>;
var newLink = document.createElement("a");
newLink.href = <?php echo json_encode($page_url); ?>;
newLink.id = "rndpic-a";
newLink.appendChild(newImg);
newLink.target = <?php echo json_encode($target); ?>;
var target = document.getElementById(<?php echo json_encode($element_name); ?>);
if (!target)
{
// Could not find #random_image. As a
// fall-back, try to find the parent of the
// <script> tag calling us.
// http://stackoverflow.com/questions/6932679/get-dom-element-where-script-tag-is
var target = document.documentElement;
while (target.childNodes.length && target.lastChild.nodeType == 1)
{
target = target.lastChild;
}
target = target.parentNode;
}
target.appendChild(newLink);
<?php
} else if ($mode === 'html') {
echo '<a id="rndpic-a" href="' . htmlspecialchars($page_url) .
'" target="' . htmlspecialchars($target) . '"><img src="'
. htmlspecialchars($image_url) . '" alt="" title="' . htmlspecialchars($comment) . '" />'
. '</a>';
}
}
}
else
{
// Silent error.
// echo "Error";
}