1<?php
2// (c) Copyright by authors of the Tiki Wiki CMS Groupware Project
3//
4// All Rights Reserved. See copyright.txt for details and a complete list of authors.
5// Licensed under the GNU LESSER GENERAL PUBLIC LICENSE. See license.txt for details.
6// $Id$
7
8$section = 'galleries';
9require_once('tiki-setup.php');
10
11$imagegallib = TikiLib::lib('imagegal');
12$categlib = TikiLib::lib('categ');
13
14$access->check_feature('feature_galleries');
15
16if (isset($_REQUEST['find'])) {
17	$find = $_REQUEST['find'];
18} else {
19	$find = '';
20}
21
22$smarty->assign('find', $find);
23
24if (! isset($_REQUEST['galleryId'])) {
25	$_REQUEST['galleryId'] = 0;
26}
27
28$smarty->assign('galleryId', $_REQUEST['galleryId']);
29
30$access->check_permission('tiki_p_list_image_galleries');
31
32// Individual permissions are checked because we may be trying to edit the gallery
33// Check here for indivdual permissions the objectType is 'image galleries' and the id is galleryId
34$smarty->assign('individual', 'n');
35
36$tikilib->get_perm_object($_REQUEST['galleryId'], 'image gallery');
37
38if (isset($_REQUEST['migrate_images_to_fgal'])) {
39	$access->check_feature('feature_file_galleries');
40	$access->check_permission('tiki_p_admin');
41
42	$fileGalLib = TikiLib::lib('filegal');
43	if ($fileGalLib->is_default_gallery_writable()) {
44		$fileGalLib->migrateFilesFromImageGalleries();
45		Feedback::success(tra('All files copied'));
46		$access->redirect('tiki-galleries.php');
47	} else {
48		Feedback::error(tr('No files migrated, default file gallery path is not writable.'));
49	}
50}
51
52$foo = parse_url($_SERVER['REQUEST_URI']);
53$foo['path'] = str_replace('tiki-galleries', 'tiki-browse_gallery', $foo['path']);
54$smarty->assign('url', $tikilib->httpPrefix() . $foo['path']);
55
56if (! isset($_REQUEST['maxRows'])) {
57	$_REQUEST['maxRows'] = $prefs['maxRowsGalleries'];
58}
59if (! isset($_REQUEST['rowImages'])) {
60	$_REQUEST['rowImages'] = $prefs['rowImagesGalleries'];
61}
62if (! isset($_REQUEST['thumbSizeX'])) {
63	$_REQUEST['thumbSizeX'] = $prefs['thumbSizeXGalleries'];
64}
65if (! isset($_REQUEST['thumbSizeY'])) {
66	$_REQUEST['thumbSizeY'] = $prefs['thumbSizeYGalleries'];
67}
68if (! isset($_REQUEST['scaleSize'])) {
69	$_REQUEST['scaleSize'] = $prefs['scaleSizeGalleries'];
70}
71
72if (isset($_REQUEST['edit']) || isset($_REQUEST['preview']) || $_REQUEST['galleryId'] == 0) {
73	if (! isset($_REQUEST['description'])) {
74		$_REQUEST['description'] = '';
75	}
76	if (! isset($_REQUEST['maxRows'])) {
77		$_REQUEST['maxRows'] = 10;
78	}
79	if (! isset($_REQUEST['rowImages'])) {
80		$_REQUEST['rowImages'] = 6;
81	}
82	if (! isset($_REQUEST['thumbSizeX'])) {
83		$_REQUEST['thumbSizeX'] = 80;
84	}
85	if (! isset($_REQUEST['thumbSizeY'])) {
86		$_REQUEST['thumbSizeY'] = 80;
87	}
88	if (! isset($_REQUEST['sortorder'])) {
89		$_REQUEST['sortorder'] = 'created';
90	}
91	if (! isset($_REQUEST['sortdirection'])) {
92		$_REQUEST['sortdirection'] = 'desc';
93	}
94	if (! isset($_REQUEST['galleryimage'])) {
95		$_REQUEST['galleryimage'] = 'first';
96	}
97	if (! isset($_REQUEST['parentgallery'])) {
98		$_REQUEST['parentgallery'] = -1;
99	}
100	if (! isset($_REQUEST['defaultscale'])) {
101		$_REQUEST['defaultscale'] = 'o';
102	}
103}
104
105
106// Init smarty variables to blank values
107//$smarty->assign('theme','');
108$smarty->assign('name', '');
109$smarty->assign('description', '');
110$smarty->assign('maxRows', $_REQUEST['maxRows']);
111$smarty->assign('rowImages', $_REQUEST['rowImages']);
112$smarty->assign('thumbSizeX', $_REQUEST['thumbSizeX']);
113$smarty->assign('thumbSizeY', $_REQUEST['thumbSizeY']);
114$smarty->assign('scaleSize', $_REQUEST['scaleSize']);
115
116$smarty->assign('public', 'n');
117$smarty->assign('edited', 'n');
118$smarty->assign('visible', 'y');
119$smarty->assign('owner', $user);
120$smarty->assign('geographic', 'n');
121$smarty->assign('edit_mode', 'n');
122
123$options_sortorder = [
124		tra('id') => 'imageId',
125		tra('Name') => 'name',
126		tra('Creation Date') => 'created',
127		tra('Owner') => 'user',
128		tra('Hits') => 'hits',
129		tra('Size') => 'filesize'
130];
131
132$smarty->assign_by_ref('options_sortorder', $options_sortorder);
133$smarty->assign('sortorder', 'imageId');
134$smarty->assign('sortorder', 'created');
135$smarty->assign('sortdirection', 'desc');
136$smarty->assign('showname', 'y');
137$smarty->assign('showimageid', 'n');
138$smarty->assign('showcategories', 'n');
139$smarty->assign('showdescription', 'n');
140$smarty->assign('showcreated', 'n');
141$smarty->assign('showuser', 'n');
142$smarty->assign('showhits', 'y');
143$smarty->assign('showxysize', 'n');
144$smarty->assign('showfilesize', 'n');
145$smarty->assign('showfilename', 'n');
146
147$options_galleryimage = [
148		tra('first uploaded image') => 'firstu',
149		tra('last uploaded image') => 'lastu',
150		tra('first image') => 'first',
151		tra('last image') => 'last',
152		tra('random image') => 'random'
153];
154
155$smarty->assign_by_ref('options_galleryimage', $options_galleryimage);
156$smarty->assign('galleryimage', 'first');
157$galleries_list = $imagegallib->list_galleries(0, -1, 'name_desc', $user);
158$smarty->assign_by_ref('galleries_list', $galleries_list['data']);
159$smarty->assign('defaultscale', 'o');
160$smarty->assign('scaleinfo', []);
161$smarty->assign('parentgallery', -1);
162
163// If we are editing an existing gallery prepare smarty variables
164if (isset($_REQUEST['edit_mode']) && $_REQUEST['edit_mode']) {
165	check_ticket('galleries');
166
167	// Get information about this galleryID and fill smarty variables
168	$smarty->assign('edit_mode', 'y');
169
170	$smarty->assign('edited', 'y');
171
172	if ($_REQUEST['galleryId'] > 0) {
173		if ($info = $imagegallib->get_gallery_info($_REQUEST['galleryId'])) {
174			$scaleinfo = $imagegallib->get_gallery_scale_info($_REQUEST['galleryId']);
175			$gallery_images = $imagegallib->get_images(0, -1, 'name_asc', false, $_REQUEST['galleryId']);
176
177			foreach ($gallery_images['data'] as $key => $item) {
178				$options_galleryimage[tra('Image') . ' ' . $item['name']] = $item['imageId'];
179			}
180
181			//$smarty->assign_by_ref('theme', $info["theme"]);
182			$smarty->assign_by_ref('name', $info['name']);
183			$smarty->assign_by_ref('description', $info['description']);
184			$smarty->assign_by_ref('maxRows', $info['maxRows']);
185			$smarty->assign_by_ref('rowImages', $info['rowImages']);
186			$smarty->assign_by_ref('thumbSizeX', $info['thumbSizeX']);
187			$smarty->assign_by_ref('thumbSizeY', $info['thumbSizeY']);
188			$smarty->assign_by_ref('public', $info['public']);
189			$smarty->assign_by_ref('visible', $info['visible']);
190			$smarty->assign_by_ref('owner', $info['user']);
191			$smarty->assign('sortorder', $info['sortorder']);
192			$smarty->assign('sortdirection', $info['sortdirection']);
193			$smarty->assign('galleryimage', $info['galleryimage']);
194			$smarty->assign('parentgallery', $info['parentgallery']);
195			$smarty->assign('showname', $info['showname']);
196			$smarty->assign('showimageid', $info['showimageid']);
197			$smarty->assign('showcategories', $info['showcategories']);
198			;
199			$smarty->assign('showdescription', $info['showdescription']);
200			$smarty->assign('showcreated', $info['showcreated']);
201			$smarty->assign('showuser', $info['showuser']);
202			$smarty->assign('showhits', $info['showhits']);
203			$smarty->assign('showxysize', $info['showxysize']);
204			$smarty->assign('showfilesize', $info['showfilesize']);
205			$smarty->assign('showfilename', $info['showfilename']);
206			$smarty->assign('defaultscale', $info['defaultscale']);
207			$smarty->assign_by_ref('geographic', $info['geographic']);
208			$smarty->assign_by_ref('scaleinfo', $scaleinfo);
209		}
210	}
211}
212
213// Process the insertion or modification of a gallery here
214$category_needed = 'n';
215if (isset($_REQUEST['edit'])
216		&& $prefs['feature_categories'] == 'y'
217		&& $prefs['feature_image_gallery_mandatory_category'] >= 0
218		&& (empty($_REQUEST['cat_categories']) || count($_REQUEST['cat_categories']) <= 0)
219	 ) {
220	$category_needed = 'y';
221} elseif (isset($_REQUEST['edit'])) {
222	check_ticket('galleries');
223	// Saving information
224	// If the user is not gallery admin
225	if ($tiki_p_admin_galleries != 'y') {
226		if ($tiki_p_create_galleries != 'y') {
227			// If you can't create a gallery then you can't edit a gallery because you can't have a gallery
228			$smarty->assign('errortype', 401);
229			$smarty->assign('msg', tra('You do not have permission to create galleries and so you cannot edit them'));
230
231			$smarty->display('error.tpl');
232			die;
233		}
234
235		// If the user can create a gallery then check if he can edit THIS gallery
236		if ($_REQUEST['galleryId'] > 0) {
237			$info = $imagegallib->get_gallery_info($_REQUEST['galleryId']);
238
239			if (! $user || $info['user'] != $user) {
240				$smarty->assign('errortype', 401);
241				$smarty->assign('msg', tra('You do not have permission to edit this gallery'));
242
243				$smarty->display('error.tpl');
244				die;
245			}
246		}
247	}
248
249	// Everything is ok so we proceed to edit the gallery
250	$smarty->assign('edit_mode', 'y');
251	//$smarty->assign_by_ref('theme',$_REQUEST['theme']);
252	$smarty->assign_by_ref('name', $_REQUEST['name']);
253	$smarty->assign_by_ref('owner', $_REQUEST['owner']);
254	$smarty->assign_by_ref('description', $_REQUEST['description']);
255	$smarty->assign_by_ref('maxRows', $_REQUEST['maxRows']);
256	$smarty->assign_by_ref('rowImages', $_REQUEST['rowImages']);
257	$smarty->assign_by_ref('thumbSizeX', $_REQUEST['thumbSizeX']);
258	$smarty->assign_by_ref('thumbSizeY', $_REQUEST['thumbSizeY']);
259	$smarty->assign('sortorder', $_REQUEST['sortorder']);
260	$smarty->assign('sortdirection', $_REQUEST['sortdirection']);
261	$smarty->assign('galleryimage', $_REQUEST['galleryimage']);
262	$smarty->assign('parentgallery', $_REQUEST['parentgallery']);
263	$smarty->assign('defaultscale', $_REQUEST['defaultscale']);
264
265	$auxarray = [
266			'showname',
267			'showimageid',
268			'showdescription',
269			'showcreated',
270			'showuser',
271			'showhits',
272			'showxysize',
273			'showfilesize',
274			'showfilename',
275			'showcategories'
276	];
277
278	foreach ($auxarray as $key => $item) {
279		if (! isset($_REQUEST[$item])) {
280			$_REQUEST[$item] = 'n';
281		}
282		$smarty->assign($item, $_REQUEST[$item]);
283	}
284
285	if (isset($_REQUEST['visible']) && $_REQUEST['visible'] == 'on') {
286		$visible = 'y';
287	} else {
288		$visible = 'n';
289	}
290
291	if (isset($_REQUEST['geographic']) && $_REQUEST['geographic'] == 'on') {
292		$geographic = 'y';
293	} else {
294		$geographic = 'n';
295	}
296
297	if (isset($_REQUEST['public']) && $_REQUEST['public'] == 'on') {
298		$public = 'y';
299	} else {
300		$public = 'n';
301	}
302
303	$gid = $imagegallib->replace_gallery(
304		$_REQUEST['galleryId'],
305		$_REQUEST['name'],
306		$_REQUEST['description'],
307		'',
308		$_REQUEST['owner'],
309		$_REQUEST['maxRows'],
310		$_REQUEST['rowImages'],
311		$_REQUEST['thumbSizeX'],
312		$_REQUEST['thumbSizeY'],
313		$public,
314		$visible,
315		$_REQUEST['sortorder'],
316		$_REQUEST['sortdirection'],
317		$_REQUEST['galleryimage'],
318		$_REQUEST['parentgallery'],
319		$_REQUEST['showname'],
320		$_REQUEST['showimageid'],
321		$_REQUEST['showdescription'],
322		$_REQUEST['showcreated'],
323		$_REQUEST['showuser'],
324		$_REQUEST['showhits'],
325		$_REQUEST['showxysize'],
326		$_REQUEST['showfilesize'],
327		$_REQUEST['showfilename'],
328		$_REQUEST['defaultscale'],
329		$geographic,
330		$_REQUEST['showcategories']
331	);
332
333	#add scales
334	if (isset($_REQUEST['scaleSize'])) {
335		if (strstr($_REQUEST['scaleSize'], ',')) {
336			$sc = explode(',', $_REQUEST['scaleSize']);
337			foreach ($sc as $thisc) {
338				$thisc = trim($thisc);
339				if (is_numeric($thisc)) {
340					$imagegallib->add_gallery_scale($gid, $thisc);
341				}
342			}
343		} elseif (is_numeric($_REQUEST['scaleSize'])) {
344			$imagegallib->add_gallery_scale($gid, $_REQUEST['scaleSize']);
345		}
346	}
347
348#remove scales
349	$scaleinfo = $imagegallib->get_gallery_scale_info($_REQUEST['galleryId']);
350
351# loop though scales to determine if a scale has to be removed
352	foreach ($scaleinfo as $sci) {
353		$removestr = 'removescale_' . $sci['scale'];
354
355		if (isset($_REQUEST[$removestr]) && $_REQUEST[$removestr] == 'on') {
356			$imagegallib->remove_gallery_scale($_REQUEST['galleryId'], $sci['scale']);
357		}
358	}
359
360	$cat_type = 'image gallery';
361	$cat_objid = $gid;
362	$cat_desc = substr($_REQUEST['description'], 0, 200);
363	$cat_name = $_REQUEST['name'];
364	$cat_href = 'tiki-browse_gallery.php?galleryId=' . $cat_objid;
365	include_once('categorize.php');
366	include_once('freetag_apply.php');
367
368	$smarty->assign('edit_mode', 'n');
369	$smarty->assign('galleryId', '');
370	$_REQUEST['galleryId'] = 0;
371}
372
373if ($category_needed == 'y') {
374	$smarty->assign_by_ref('name', $_REQUEST['name']);
375	$smarty->assign_by_ref('description', $_REQUEST['description']);
376	$smarty->assign_by_ref('maxRows', $_REQUEST['maxRows']);
377	$smarty->assign_by_ref('rowImages', $_REQUEST['rowImages']);
378	$smarty->assign_by_ref('thumbSizeX', $_REQUEST['thumbSizeX']);
379	$smarty->assign_by_ref('thumbSizeY', $_REQUEST['thumbSizeY']);
380	$smarty->assign('sortorder', $_REQUEST['sortorder']);
381	$smarty->assign('sortdirection', $_REQUEST['sortdirection']);
382	$smarty->assign('galleryimage', $_REQUEST['galleryimage']);
383	$smarty->assign('parentgallery', $_REQUEST['parentgallery']);
384	$smarty->assign('defaultscale', $_REQUEST['defaultscale']);
385
386	$auxarray = [
387			'showname',
388			'showimageid',
389			'showdescription',
390			'showcreated',
391			'showuser',
392			'showhits',
393			'showxysize',
394			'showfilesize',
395			'showfilename',
396			'showcategories'
397			];
398
399	foreach ($auxarray as $key => $item) {
400		if (! isset($_REQUEST[$item])) {
401			$_REQUEST[$item] = 'n';
402		}
403		$smarty->assign($item, $_REQUEST[$item]);
404	}
405	if (isset($_REQUEST['visible']) && $_REQUEST['visible'] == 'on') {
406		$visible = 'y';
407	} else {
408		$visible = 'n';
409	}
410	$smarty->assign_by_ref('visible', $visible);
411
412	if (isset($_REQUEST['geographic']) && $_REQUEST['geographic'] == 'on') {
413		$geographic = 'y';
414	} else {
415		$geographic = 'n';
416	}
417
418	$smarty->assign_by_ref('geographic', $geographic);
419
420	if (isset($_REQUEST['public']) && $_REQUEST['public'] == 'on') {
421		$public = 'y';
422	} else {
423		$public = 'n';
424	}
425	$smarty->assign_by_ref('public', $public);
426	$smarty->assign('edit_mode', 'y');
427}
428
429if (isset($_REQUEST['removegal'])) {
430	if ($tiki_p_admin_galleries != 'y') {
431		$info = $imagegallib->get_gallery_info($_REQUEST['removegal']);
432
433		if (! $user || $info['user'] != $user) {
434			$smarty->assign('errortype', 401);
435			$smarty->assign('msg', tra('You do not have permission to remove this gallery'));
436
437			$smarty->display('error.tpl');
438			die;
439		}
440	}
441	$access->check_authenticity();
442	$imagegallib->remove_gallery($_REQUEST['removegal']);
443}
444$smarty->assign('category_needed', $category_needed);
445
446if (! isset($_REQUEST['sort_mode'])) {
447	$sort_mode = 'name_asc';
448} else {
449	$sort_mode = $_REQUEST['sort_mode'];
450}
451
452$smarty->assign_by_ref('sort_mode', $sort_mode);
453
454if (! isset($_REQUEST['offset'])) {
455	$offset = 0;
456} else {
457	$offset = $_REQUEST['offset'];
458}
459
460$smarty->assign_by_ref('offset', $offset);
461
462// Get the list of libraries available for this user (or public galleries)
463$imagegallib = TikiLib::lib('imagegal');
464
465$galleries = $imagegallib->list_galleries($offset, $maxRecords, $sort_mode, 'admin', $find);
466Perms::bulk([ 'type' => 'image gallery' ], 'object', $galleries, 'galleryId');
467
468$smarty->assign('filter', '');
469if (! empty($_REQUEST['filter'])) {
470	$smarty->assign('filter', $_REQUEST['filter']);
471}
472
473
474$temp_max = count($galleries['data']);
475for ($i = 0; $i < $temp_max; $i++) {
476	$galperms = Perms::get(
477		[
478			'type' => 'image gallery',
479			'object' => $galleries['data'][$i]['galleryId']
480		]
481	);
482
483	// check if top gallery (has no parents)
484	$info = $imagegallib->get_gallery_info($galleries['data'][$i]['galleryId']);
485	if ($info['parentgallery'] == -1) {
486		$galleries['data'][$i]['topgal'] = 'y';
487	} else {
488		$galleries['data'][$i]['topgal'] = 'n';
489	}
490
491	// check if has subgalleries (parent of any children)
492	$maxImages = 1;
493
494	$subgals = $imagegallib->get_subgalleries(
495		$offset,
496		$maxImages,
497		$sort_mode,
498		'',
499		$galleries['data'][$i]['galleryId']
500	);
501
502	if (count($subgals['data']) > 0) {
503		$galleries['data'][$i]['parentgal'] = 'y';
504	} else {
505		$galleries['data'][$i]['parentgal'] = 'n';
506	}
507
508	$galleries['data'][$i]['individual_tiki_p_view_image_gallery'] = $galperms->view_image_gallery ? 'y' : 'n';
509	$galleries['data'][$i]['individual_tiki_p_upload_images'] = $galperms->upload_images ? 'y' : 'n';
510	$galleries['data'][$i]['individual_tiki_p_create_galleries'] = $galperms->create_galleries ? 'y' : 'n';
511}
512
513$smarty->assign_by_ref('galleries', $galleries['data']);
514$smarty->assign_by_ref('cant', $galleries['cant']);
515
516$cat_type = 'image gallery';
517$cat_objid = $_REQUEST['galleryId'];
518include_once('categorize_list.php');
519include_once('freetag_list.php');
520
521$defaultRows = 5;
522
523include_once('tiki-section_options.php');
524ask_ticket('galleries');
525
526$perms = Perms::get();
527$smarty->assign('display_migrate_filegal', ($perms->admin == 'y' && $prefs['feature_file_galleries'] == 'y') ? true : false);
528
529// Display the template
530$smarty->assign('mid', 'tiki-galleries.tpl');
531$smarty->display('tiki.tpl');
532