织梦dedecms如何完成自动定时更新首页面任务

思路
每天早上6:00登陆到后台,手动更新一次, 这样做太麻烦.
使用 dede后台->系统->任务计划管理 , 这样做我测试没有成功,并且就算是成功,提供的功能也比较弱.
通过使用firebug ,我们发现更新首页其实就是向 makehtml_homepage.php 文件发出POST请求,所以我们可以直接
请求 makehtml_homepage.php 来完成更新首页面的功能
步骤
考虑安全性,我们单独写一个文件 makehtml_homepage_hsp1213.php 和 config_hsp1213.php
,这两个文件是在 dede/manage/makehtml_homepage.php 和 dede/manage/config.php文件基础上
修改的.
///这里主要的问题是这两个文件需要考虑是系统在执行,而不是管理员真正登录到后台手动更新,因此
//需要人为构建一个管理员 对象,并判断请求方是不是 127.0.0.1

dede/manage/config_hsp1213.php

safeCheck = FALSE;
$dsql->SetLongLink();
$cfg_admin_skin = 1; // 后台管理风格

if(file_exists(DEDEDATA.’/admin/skin.txt’))
{
$skin = file_get_contents(DEDEDATA.’/admin/skin.txt’);
$cfg_admin_skin = !in_array($skin, array(1,2,3,4))? 1 : $skin;
}

//获得当前脚本名称,如果你的系统被禁用了$_SERVER变量,请自行更改这个选项
$dedeNowurl = $s_scriptName = ”;
$isUrlOpen = @ini_get(‘allow_url_fopen’);
$dedeNowurl = GetCurUrl();
$dedeNowurls = explode(‘?’, $dedeNowurl);
$s_scriptName = $dedeNowurls[0];
$cfg_remote_site = empty($cfg_remote_site)? ‘N’ : $cfg_remote_site;

//检验用户登录状态
$cuserLogin = new userLogin();

/*echo “

";
var_dump($cuserLogin);
echo "

“;
exit;*/

if($_SERVER[‘REMOTE_ADDR’]==’127.0.0.1′){

$cuserLogin->userName=’admin’;
$cuserLogin->userID=”1″;
$cuserLogin->userType=”10″;
$cuserLogin->userChannel=”0″;
$cuserLogin->userPurview=”admin_AllowAll”;
$cuserLogin->keepUserIDTag=”dede_admin_id”;
$cuserLogin->keepUserTypeTag=”dede_admin_type”;
$cuserLogin->keepUserChannelTag=”dede_admin_channel”;
$cuserLogin->keepUserNameTag=”dede_admin_name”;
$cuserLogin->keepUserPurviewTag=”dede_admin_purview”;
$cuserLogin->keepAdminStyleTag=”dede_admin_style”;
$cuserLogin->adminStyle=”newdedecms”;

}
/*原始的管理员对象
object(userLogin)#2 (14) {
[“userName”]=>
string(5) “admin”
[“userPwd”]=>
string(0) “”
[“userID”]=>
string(1) “1”
[“adminDir”]=>
NULL
[“userType”]=>
string(2) “10”
[“userChannel”]=>
string(1) “0”
[“userPurview”]=>
string(15) “admin_AllowAll ”
[“keepUserIDTag”]=>
string(13) “dede_admin_id”
[“keepUserTypeTag”]=>
string(15) “dede_admin_type”
[“keepUserChannelTag”]=>
string(18) “dede_admin_channel”
[“keepUserNameTag”]=>
string(15) “dede_admin_name”
[“keepUserPurviewTag”]=>
string(18) “dede_admin_purview”
[“keepAdminStyleTag”]=>
string(16) “dede_admin_style”
[“adminStyle”]=>
string(10) “newdedecms”
}*/
if($cuserLogin->getUserID()==-1)
{
header(“location:login.php?gotopage=”.urlencode($dedeNowurl));
exit();
}

if($cfg_dede_log==’Y’)
{
$s_nologfile = ‘_main|_list’;
$s_needlogfile = ‘sys_|file_’;
$s_method = isset($_SERVER[‘REQUEST_METHOD’]) ? $_SERVER[‘REQUEST_METHOD’] : ”;
$s_query = isset($dedeNowurls[1]) ? $dedeNowurls[1] : ”;
$s_scriptNames = explode(‘/’, $s_scriptName);
$s_scriptNames = $s_scriptNames[count($s_scriptNames)-1];
$s_userip = GetIP();
if( $s_method==’POST’ || (!preg_match(“#”.$s_nologfile.”#i”, $s_scriptNames) && $s_query!=”) || preg_match(“#”.$s_needlogfile.”#i”,$s_scriptNames) )
{
$inquery = “INSERT INTO `#@__log`(adminid,filename,method,query,cip,dtime)
VALUES (‘”.$cuserLogin->getUserID().”‘,'{$s_scriptNames}’,'{$s_method}’,'”.addslashes($s_query).”‘,'{$s_userip}’,'”.time().”‘);”;
$dsql->ExecuteNoneQuery($inquery);
}
}

//启用远程站点则创建FTP类
if($cfg_remote_site==’Y’)
{
require_once(DEDEINC.’/ftp.class.php’);
if(file_exists(DEDEDATA.”/cache/inc_remote_config.php”))
{
require_once DEDEDATA.”/cache/inc_remote_config.php”;
}
if(empty($remoteuploads)) $remoteuploads = 0;
if(empty($remoteupUrl)) $remoteupUrl = ”;
$config = array(
‘hostname’ => $GLOBALS[‘cfg_ftp_host’],
‘username’ => $GLOBALS[‘cfg_ftp_user’],
‘password’ => $GLOBALS[‘cfg_ftp_pwd’],
‘debug’ => ‘TRUE’
);
$ftp = new FTP($config);

//初始化FTP配置
if($remoteuploads==1){
$ftpconfig = array(
‘hostname’=>$rmhost,
‘port’=>$rmport,
‘username’=>$rmname,
‘password’=>$rmpwd
);
}
}

//管理缓存、管理员频道缓存
$cache1 = DEDEDATA.’/cache/inc_catalog_base.inc’;
if(!file_exists($cache1)) UpDateCatCache();
$cacheFile = DEDEDATA.’/cache/admincat_’.$cuserLogin->userID.’.inc’;
if(file_exists($cacheFile)) require_once($cacheFile);

//更新服务器
require_once (DEDEDATA.’/admin/config_update.php’);

/**
* 更新栏目缓存
*
* @access public
* @return void
*/
function UpDateCatCache()
{
global $dsql, $cfg_multi_site, $cache1, $cacheFile, $cuserLogin;
$cache2 = DEDEDATA.’/cache/channelsonlist.inc’;
$cache3 = DEDEDATA.’/cache/channeltoplist.inc’;
$dsql->SetQuery(“SELECT id,reid,channeltype,issend,typename FROM `#@__arctype`”);
$dsql->Execute();
$fp1 = fopen($cache1,’w’);
$phph = ‘?’;
$fp1Header = “<{$phph}php\r\nglobal \$cfg_Cs;\r\n\$cfg_Cs=array();\r\n"; fwrite($fp1,$fp1Header); while($row=$dsql->GetObject())
{
// 将typename缓存起来
$row->typename = base64_encode($row->typename);
fwrite($fp1,”\$cfg_Cs[{$row->id}]=array({$row->reid},{$row->channeltype},{$row->issend},'{$row->typename}’);\r\n”);
}
fwrite($fp1, “{$phph}>”);
fclose($fp1);
$cuserLogin->ReWriteAdminChannel();
@unlink($cache2);
@unlink($cache3);
}

// 清空选项缓存
function ClearOptCache()
{
$tplCache = DEDEDATA.’/tplcache/’;
$fileArray = glob($tplCache.”inc_option_*.inc”);
if (count($fileArray) > 1)
{
foreach ($fileArray as $key => $value)
{
if (file_exists($value)) unlink($value);
else continue;
}
return TRUE;
}
return FALSE;
}

/**
* 更新会员模型缓存
*
* @access public
* @return void
*/
function UpDateMemberModCache()
{
global $dsql;
$cachefile = DEDEDATA.’/cache/member_model.inc’;

$dsql->SetQuery(“SELECT * FROM `#@__member_model` WHERE state=’1′”);
$dsql->Execute();
$fp1 = fopen($cachefile,’w’);
$phph = ‘?’;
$fp1Header = “<{$phph}php\r\nglobal \$_MemberMod;\r\n\$_MemberMod=array();\r\n"; fwrite($fp1,$fp1Header); while($row=$dsql->GetObject())
{
fwrite($fp1,”\$_MemberMod[{$row->id}]=array(‘{$row->name}’,'{$row->table}’);\r\n”);
}
fwrite($fp1,”{$phph}>”);
fclose($fp1);
}

/**
* 引入模板文件
*
* @access public
* @param string $filename 文件名称
* @param bool $isabs 是否为管理目录
* @return string
*/
function DedeInclude($filename, $isabs=FALSE)
{
return $isabs ? $filename : DEDEADMIN.’/’.$filename;
}

/**
* 获取当前用户的ftp站点
*
* @access public
* @param string $current 当前站点
* @param string $formname 表单名称
* @return string
*/
function GetFtp($current=”, $formname=”)
{
global $dsql;
$formname = empty($formname)? ‘serviterm’ : $formname;
$cuserLogin = new userLogin();
$row=$dsql->GetOne(“SELECT servinfo FROM `#@__multiserv_config`”);
$row[‘servinfo’]=trim($row[‘servinfo’]);
if(!empty($row[‘servinfo’])){
$servinfos = explode(“\n”, $row[‘servinfo’]);
$select=””;
echo ‘“;
}
}
helper(‘cache’);
/**
* 根据用户mid获取用户名称
*
* @access public
* @param int $mid 用户ID
* @return string
*/
if(!function_exists(‘GetMemberName’)){
function GetMemberName($mid=0)
{
global $dsql;
$rs = GetCache(‘memberlogin’, $mid);
if( empty($rs) )
{
$rs = $dsql->GetOne(“SELECT * FROM `#@__member` WHERE mid='{$mid}’ “);
SetCache(‘memberlogin’, $mid, $rs, 1800);
}
return $rs[‘uname’];
}
}

dede/manage/makehtml_homepage_hsp1213.php

SetTemplet($cfg_basedir.$cfg_templets_dir.”/”.$templet);
$pv->Display();
exit();
}
else if($dopost==”make”)
{
$remotepos = empty($remotepos)? ‘/index.html’ : $remotepos;
$isremote = empty($isremote)? 0 : $isremote;
$serviterm = empty($serviterm)? “” : $serviterm;
$homeFile = DEDEADMIN.”/”.$position;
$homeFile = str_replace(“\\”,”/”,$homeFile);
$homeFile = str_replace(“//”,”/”,$homeFile);
$fp = fopen($homeFile,”w”) or die(“你指定的文件名有问题,无法创建文件”);
fclose($fp);
if($saveset==1)
{
$iquery = “UPDATE `#@__homepageset` SET templet=’$templet’,position=’$position’ “;
$dsql->ExecuteNoneQuery($iquery);
}
// 判断首页生成模式
if ($showmod == 1)
{
// 需要生成静态
$templet = str_replace(“{style}”, $cfg_df_style, $templet);
$pv = new PartView();
$GLOBALS[‘_arclistEnv’] = ‘index’;
$pv->SetTemplet($cfg_basedir.$cfg_templets_dir.”/”.$templet);
$pv->SaveToHtml($homeFile);
echo “成功更新主页HTML:”.$homeFile.”
浏览…
“;
} else {
// 动态浏览
if (file_exists($homeFile)) @unlink($homeFile);
echo “采用动态浏览模式:浏览…
“;
}

$iquery = “UPDATE `#@__homepageset` SET showmod=’$showmod'”;
$dsql->ExecuteNoneQuery($iquery);

if($serviterm ==””)
{
$config=array();
} else {
list($servurl, $servuser, $servpwd) = explode(‘,’,$serviterm);
$config=array( ‘hostname’ => $servurl, ‘username’ => $servuser,
‘password’ => $servpwd,’debug’ => ‘TRUE’);
}
//如果启用远程站点则上传
if($cfg_remote_site==’Y’ && $showmod==1)
{
if($ftp->connect($config) && $isremote == 1)
{
if($ftp->upload($position, $remotepos, ‘ascii’)) echo “远程发布成功!”.”
“;
}
}
exit();
}
$row = $dsql->GetOne(“SELECT * FROM #@__homepageset”);
include DedeInclude(‘templets/makehtml_homepage.htm’);

(3)然后通过 编写一个 task1.bat 内容如下
C:/wamp/Apache2/bin/ab.exe http://localhost/dede/manage/makehtml_homepage_hsp1213.php

(4)在windows使用任务计划控制,在linux下使用 crontab 来控制定时任务

打赏作者

《织梦dedecms如何完成自动定时更新首页面任务》上有2条评论

发表评论

电子邮件地址不会被公开。