<?
define ('IMGDIR', '/home/devel/public_html/domain.com/
public
/images/slideshow/');
define ('WEBIMGDIR', '/images/slideshow/');
define ('SS_SESSNAME', 'slideshow_sess');
$err
= '';
session_name(SS_SESSNAME);
session_start();
$ss
=
new
slideshow(
$err
);
if
((
$err
=
$ss
->init()) != '')
{
header('HTTP/1.1 500 Internal Server Error');
echo
$err
;
exit
();
}
$ss
->get_images();
list(
$curr
,
$caption
,
$first
,
$prev
,
$next
,
$last
) =
$ss
->run();
class
slideshow
{
private
$files_arr
= NULL;
private
$err
= NULL;
public
function
__construct(&
$err
)
{
$this
->files_arr =
array
();
$this
->err =
$err
;
}
public
function
init()
{
if
(!
$this
->dir_exists())
{
return
'Error retrieving images, missing directory';
}
return
'';
}
public
function
get_images()
{
if
(isset(
$_SESSION
['imgarr']))
{
$this
->files_arr =
$_SESSION
['imgarr'];
}
else
{
if
(
$dh
= opendir(IMGDIR))
{
while
(false !== (
$file
= readdir(
$dh
)))
{
if
(preg_match('/^.*\.(jpg|jpeg|gif|png)$/i',
$file
))
{
$this
->files_arr[] =
$file
;
}
}
closedir
(
$dh
);
}
$_SESSION
['imgarr'] =
$this
->files_arr;
}
}
public
function
run()
{
$curr
= 1;
$last
=
count
(
$this
->files_arr);
if
(isset(
$_GET
['img']))
{
if
(preg_match('/^[0-9]+$/',
$_GET
['img']))
$curr
= (int)
$_GET
['img'];
if
(
$curr
<= 0 ||
$curr
>
$last
)
$curr
= 1;
}
if
(
$curr
<= 1)
{
$prev
=
$curr
;
$next
=
$curr
+ 1;
}
else
if
(
$curr
>=
$last
)
{
$prev
=
$last
- 1;
$next
=
$last
;
}
else
{
$prev
=
$curr
- 1;
$next
=
$curr
+ 1;
}
$caption
=
str_replace
('-', ' ',
$this
->files_arr[
$curr
- 1]);
$caption
=
str_replace
('_', ' ',
$caption
);
$caption
= preg_replace('/\.(jpe?g|gif|png)$/i', '',
$caption
);
$caption
= ucfirst(
$caption
);
return
array
(
$this
->files_arr[
$curr
- 1],
$caption
, 1,
$prev
,
$next
,
$last
);
}
private
function
dir_exists()
{
return
file_exists
(IMGDIR);
}
}
?>