<?phpnamespace Common;
class
StaticHtml{
private
static
$_instance
= null;
private
$_needStatic
= false;
private
$_needDeleteStatic
= false;
private
$_hasStaticed
= true;
private
$_controller
= null;
private
$_action
= null;
//
private
$_staticAgain
= false;
private
$_save_path
= null;
private
$_conf
=
array
(
'files_per_directory'
=> 100
);
private
$_staticList
=
array
(
//
'Base'
=>
array
(
//
'aaa'
=>
array
(
//
'save_path'
=>
'/StaticHtml/Base/aaa/'
,
//
'static_base'
=>
'id'
,
//
'alias'
=>
'aaa'
// )// )
'Mynotes'
=>
array
(
'look_notes'
=>
array
(
'save_path'
=>
'/StaticHtml/Mynotes/look_notes/'
,
'static_base'
=>
'id'
,
'alias'
=>
'note'
),
'add_personal_notes'
=>
array
(
'save_path'
=>
'/StaticHtml/Mynotes/'
,
'alias'
=>
'note-add'
) ),
'Resource'
=>
array
(
'allResource'
=>
array
(
'save_path'
=>
'/StaticHtml/Resource/'
,
'alias'
=>
'allResource'
),
'resource_add'
=>
array
(
'save_path'
=>
'/StaticHtml/Resource/'
,
'alias'
=>
'resourceAdd'
) ),
'Thing'
=>
array
(
'suggestion_of_lecture'
=>
array
(
'save_path'
=>
'/StaticHtml/Lecture/'
,
'alias'
=>
'voteLecture'
) ),
'Passwordfix'
=>
array
(
'index'
=>
array
(
'save_path'
=>
'/StaticHtml/Information/'
,
'alias'
=>
'updatePassword'
) ),
'Information'
=>
array
(
'index'
=>
array
(
'save_path'
=>
'/StaticHtml/Information/'
,
'static_base'
=>
'user_id'
,
'alias'
=>
'information'
) ),
'Courseinfo'
=>
array
(
'course_show'
=>
array
(
'save_path'
=>
'/StaticHtml/Information/'
,
'static_base'
=>
'user_id'
,
'alias'
=>
'course'
) ) );
private
$_deleteList
=
array
(
//
'Base'
=>
array
(
//
'bbb'
=>
array
(
//
'save_path'
=>
'/StaticHtml/Base/aaa/'
,
//
'static_base'
=>
'id'
,
//
'alias'
=>
'aaa'
// )// )
'Mynotes'
=>
array
(
'edits_notes'
=>
array
(
'save_path'
=>
'/StaticHtml/Mynotes/look_notes/'
,
'static_base'
=>
'id'
,
'alias'
=>
'note'
) ),
'Information'
=>
array
(
'save_user_info'
=>
array
(
'save_path'
=>
'/StaticHtml/Information/'
,
'static_base'
=>
'user_id'
,
'alias'
=>
'information'
) ),
'Courseinfo'
=>
array
(
'course_update'
=>
array
(
'save_path'
=>
'/StaticHtml/Information/'
,
'static_base'
=>
'user_id'
,
'alias'
=>
'course'
) ) );
private
function
__construct(){
$this
->needStatic();
$this
->hasStaticed();
$this
->needDeleteStatic();
}
private
function
needDeleteStatic(){
if
(
$this
->_needDeleteStatic){
$save_path
=
$this
->getSavePath(
$this
->_deleteList[
$this
->_controller][
$this
->_action]);
$this
->_hasStaticed = false;
if
(
file_exists
(ROOT_PATH.
$save_path
)){
$this
->_hasStaticed = true; }//
$this
->_staticAgain =
$this
->_deleteList[
$this
->_controller][
$this
->_action][
'visitAgain'
];
$this
->_save_path = ROOT_PATH.
$save_path
; } }
public
static
function
getInstance(){
if
(!(self::
$_instance
instanceof
self)){ self::
$_instance
=
new
self(); }
return
self::
$_instance
; }
private
function
hasStaticed(){
if
(
$this
->_needStatic){
$save_path
=
$this
->getSavePath(
$this
->_staticList[
$this
->_controller][
$this
->_action]);
if
(!
file_exists
(ROOT_PATH.
$save_path
)){
$this
->_hasStaticed = false; ob_start(); }
else
{ header(
"location: http://"
.
$_SERVER
[
'HTTP_HOST'
].dirname(
$_SERVER
[
'SCRIPT_NAME'
]).
$save_path
); }
$this
->_save_path = ROOT_PATH.
$save_path
; } }
private
function
getSavePath(
$conf
){
if
(!isset(
$conf
[
'static_base'
])){
$save_path
=
$conf
[
'save_path'
];
$save_path
.=
$conf
[
'alias'
].
'.html'
; }
else
{
if
(
$conf
[
'static_base'
] ==
'user_id'
){
$id
= (int)
$_SESSION
[
'logined_user'
][
'id'
]; }
else
{
if
(IS_GET){
$id
=
$_GET
[
$conf
[
'static_base'
]]; }
else
{
$id
=
$_POST
[
$conf
[
'static_base'
]]; } }
$save_path
=
$conf
[
'save_path'
];
$directory_id
=
ceil
(
$id
/
$this
->_conf[
'files_per_directory'
]);
$save_path
.=
$directory_id
.
'/'
;
if
(
$conf
[
'alias'
]){
$fileName
=
$conf
[
'alias'
].
'-'
; }
$fileName
.=
$id
.
'.html'
;
$save_path
.=
$fileName
; }
return
$save_path
; }
private
function
needStatic(){
$url
=
explode
(
'/'
,__ACTION__);
$this
->_controller =
$url
[4];
$this
->_action =
$url
[5];
if
(isset(
$this
->_staticList[
$this
->_controller]) && isset(
$this
->_staticList[
$this
->_controller][
$this
->_action])){
$this
->_needStatic = true; }
if
(isset(
$this
->_deleteList[
$this
->_controller]) && isset(
$this
->_deleteList[
$this
->_controller][
$this
->_action])){
$this
->_needDeleteStatic = true; } }
public
function
_static(){
if
(
$this
->_needStatic && !
$this
->_hasStaticed){
$html
= ob_get_contents();
$this
->_mkdir(dirname(
$this
->_save_path));
file_put_contents
(
$this
->_save_path,
$html
); }
if
(
$this
->_needDeleteStatic &&
$this
->_hasStaticed){ unlink(
$this
->_save_path);
} }
private
function
_mkdir(
$path
){
if
(!
file_exists
(
$path
)){
$this
->_mkdir(dirname(
$path
));
mkdir
(
$path
, 0777); } }}?>