-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimg.inc.php
271 lines (216 loc) · 9.83 KB
/
img.inc.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
<?php
// Copyright (c) Claus Tondering. E-mail: [email protected].
//
// This code is distributed under an MIT License:
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
// associated documentation files (the "Software"), to deal in the Software without restriction,
// including without limitation the rights to use, copy, modify, merge, publish, distribute,
// sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or
// substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
// BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//////////////////////////////////////////////////////////////////////
// Data part
//////////////////////////////////////////////////////////////////////
require_once 'html.inc.php';
require_once 'database.inc.php';
// $only_used is true if we are only interested in categories that are actually used for an image.
function find_allcats($only_used = false)
{
global $db_prefix;
$res = exec_sql("SELECT * from {$db_prefix}categories");
// $allcats will contain an entry for each category, indexed by the category id
// $allcats[id]->values will contain an entry for each category value, indexed by the value
$allcats = array();
while ($row = mysqli_fetch_object($res)) {
$row->values = array();
$useval = $row->isstring ? 'stringval' : 'intval';
$res2 = exec_sql("SELECT $useval as value, {$useval}_high as value_high, name "
. "FROM {$db_prefix}catval WHERE category=$row->id "
. "ORDER BY value, IF(ISNULL(value_high),1,0), value_high;"); // Sorts null after non-null value_high
while ($row2 = mysqli_fetch_object($res2))
if ($only_used && is_null($row2->value_high)) {
// Only include this category if it is used
$val = $row->isstring ? "'$row2->value'" : $row2->value;
$res3 = exec_sql("SELECT * FROM {$db_prefix}piccat WHERE catid=$row->id AND $useval=$val");
if (mysqli_num_rows($res3)>0)
$row->values[] = $row2;
}
else
$row->values[] = $row2;
$allcats[$row->id] = $row;
}
return $allcats;
}
/* Finds value range and name from a list of category values */
function find_cat($allcatvals, $catid)
{
foreach ($allcatvals as $a)
if ($a->value==$catid && is_null($a->value_high))
return $a;
return null;
}
function num_published_pics()
{
global $db_prefix;
$res = exec_sql("SELECT COUNT(*) c FROM {$db_prefix}photos WHERE published");
if ($row = mysqli_fetch_object($res))
return $row->c;
else
return 0;
}
function strip_links($orig)
{
return preg_replace('/{([\\wæøåÆØÅ ]+)\|([\\w_\-\.]+)}/', '$1', $orig);
}
function replace_links_json($orig)
{
global $db_prefix;
$num_links = preg_match_all('/{([\\wæøåÆØÅ ]+)\|([\\w_\-\.]+)}/', $orig, $match);
for ($i=0; $i<$num_links; ++$i) {
$text = $match[1][$i];
$picfile = $match[2][$i];
$orig = preg_replace("/\{$text\|$picfile\}/", "[$picfile]" , $orig);
}
return $orig;
}
function replace_links($orig)
{
global $db_prefix;
$num_links = preg_match_all('/{([\\wæøåÆØÅ ]+)\|([\\w_\-\.]+)}/', $orig, $match);
for ($i=0; $i<$num_links; ++$i) {
$text = $match[1][$i];
$picfile = $match[2][$i];
$res = exec_sql("SELECT pic_no FROM {$db_prefix}photos WHERE filename='$picfile'");
if ($row = mysqli_fetch_object($res))
$orig = preg_replace("/\{$text\|$picfile\}/", "<a target=\"_blank\" href=\"link.php?picno=$row->pic_no\">$text</a>", $orig);
else
$orig = preg_replace("/\{$text\|$picfile\}/", "$text" , $orig);
}
return $orig;
}
function find_pics($allids, $allcats, $num_pics, $cur, $max_per_page)
{
global $db_prefix;
$allpics = array();
if ($num_pics>0) {
$res = exec_sql("SELECT * FROM {$db_prefix}photos WHERE id IN (" . implode(',',$allids) . ") ORDER BY pic_no LIMIT $cur,$max_per_page");
while ($row = mysqli_fetch_object($res)) {
$allpics[] = $row;
if (is_null($row->description))
$row->description="";
$row->shortdesc = strip_links(strip_tags($row->description));
if (utf8_strlen($row->shortdesc)>40)
$row->shortdesc = substr($row->shortdesc,0,utf8_step($row->shortdesc,37)) . '...';
if (substr($row->description,0,2)=='<p')
$row->longdesc = $row->description;
else
$row->longdesc = shtml('p',$row->description); // Make sure $row->longdesc is embedded in <p>..</p>
$row->longdesc = replace_links($row->longdesc);
$longdescs = array();
$res2 = exec_sql("SELECT * FROM {$db_prefix}piccat WHERE picid=$row->id");
while ($row2 = mysqli_fetch_object($res2)) {
$thiscat = $allcats[$row2->catid];
if ($thiscat->display) {
$catval = find_cat($thiscat->values, is_null($row2->intval) ? $row2->stringval : $row2->intval);
if ($catval) {
if (isset($longdescs[$row2->catid]))
$longdescs[$row2->catid] .= '<br/>' . shtml('b',$thiscat->name . ': ') . $catval->name;
else
$longdescs[$row2->catid] = '<br/>' . shtml('b',$thiscat->name . ': ') . $catval->name;
}
}
}
ksort($longdescs);
foreach ($longdescs as $ld)
$row->longdesc .= $ld;
if (!is_null($row->date))
$row->longdesc .= '<br/>' . shtml('b','Date taken: ') . substr($row->date,0,10);
$res2 = exec_sql("SELECT name from {$db_prefix}authors WHERE $row->pic_no>=range_low AND $row->pic_no<=range_high");
if ($row2 = mysqli_fetch_object($res2))
$row->longdesc .= '<br/>'. shtml('b','Photographer: ') . $row2->name;
$row->longdesc = htmlspecialchars($row->longdesc);
}
}
return $allpics;
}
function mk_header($allpics, $num_pics, $max_per_page)
{
if ($num_pics>1) {
$view_num_pics = count($allpics);
return shtml_class('h1','float',"$num_pics pictures found"
. ($num_pics>$view_num_pics ? " ($view_num_pics displayed)" : ''));
}
else
return shtml_class('h1','float','1 picture found');
}
function mk_pageselector($num_pics, $max_per_page, $cur, $url)
{
$res = '<div id="pageselector">';
if ($num_pics>$max_per_page) {
$res .= '<p>Page: ';
if ($cur>0)
$res .= shtml_a("$url?cur=" . max($cur-$max_per_page,0), 'Prev');
for ($i=0; $i<$num_pics; $i+=$max_per_page) {
if ($i == $cur)
$res .= ' ' . shtml('b',floor($i/$max_per_page)+1);
else
$res .= ' ' . shtml_a("$url?cur=$i", floor($i/$max_per_page)+1);
}
if ($cur+$max_per_page<$num_pics)
$res .= ' ' . shtml_a("$url?cur=" . ($cur+$max_per_page), 'Next');
$res .= '</p>';
}
$res .= '</div>';
return $res;
}
function show_one_pic($thispic, $dirbig, $dir600, $dir160, $published, $extrafun)
{
html_class_b('td','cell');
html_class_b('div','shadow1');
html_class_b('div','thumbnail');
if ($published==$thispic->published) {
html_attr('a',"class=\"img1\" href=\"$dir600/$thispic->filename\" title=\"$thispic->longdesc\"",
shtml_attr('span','title="Click to view larger picture"',
shtml_attr_1('img',"class=\"shadow\" alt=\"Img alt\" src=\"$dir160/$thispic->filename\"")));
}
elseif ($thispic->published)
html_attr_1('img', 'class="shadow" alt="Img alt" src="images/published.png"');
else
html_attr_1('img', 'class="shadow" alt="Img alt" src="images/unpublished.png"');
html_e('div');
html_class('div','desc',$thispic->shortdesc);
html_attr('div',"class=\"ui-icon ui-icon-plus\" onclick=\"toggleExtra('#photoreveal-$thispic->id',this);\"");
html_attr_b('div',"id=\"photoreveal-$thispic->id\" style=\"display:none;\"");
print $extrafun($thispic, $dirbig, $dir600, $dir160, $published);
global $credentials;
if (!is_null($credentials->user))
html_class('p','center',shtml('i',$thispic->filename));
html_e('div');
html_e('div');
html_e('td');
}
function show_all_pics($allpics, $max_per_line, $dirbig, $dir600, $dir160, $published, $extrafun)
{
$view_num_pics = count($allpics);
html_b('table');
for ($row=0; $row<$view_num_pics; $row += $max_per_line) {
html_b('tr');
for ($col=0; $col<$max_per_line && $row+$col<$view_num_pics; ++$col) {
$thispic = $allpics[$row+$col];
show_one_pic($thispic, $dirbig, $dir600, $dir160, $published, $extrafun);
}
while (++$col<=$max_per_line)
html('td','');
html_e('tr');
}
html_e('table');
}