PHPCMS广告模块详细分析——广告的生成
发布:smiling 来源: PHP粉丝网 添加日期:2014-10-22 16:20:53 浏览: 评论:0
广告位模版配置文件在缓存中,广告的模版存在于phpcms\templates/default\poster\ 中,它和更新js功能有及其密切的关系.
下面开始分析广告的js是怎么生成的.
首先看位于 cache/poster_js下的一个已经生成的js文件,名为1.js的文件时对应的数据,位于poster_space表中id为1的记录,因为这条记录中path的值为 poster_js/1.js
- function PCMSAD(PID) {
- this.ID = PID;
- this.PosID = 0;
- this.ADID = 0;
- this.ADType = "";
- this.ADName = "";
- this.ADContent = "";
- this.PaddingLeft = 0;
- this.PaddingTop = 0;
- this.Wspaceidth = 0;
- this.Height = 0;
- this.IsHitCount = "N";
- this.UploadFilePath = "";
- this.URL = "";
- this.SiteID = 0;
- this.ShowAD = showADContent;
- this.Stat = statAD;
- }
- function statAD() {
- var new_element = document.createElement("script");
- new_element.type = "text/javascript";
- new_element.src="http://localhost/phpcms952/index.php?m=poster&c=index&a=show&siteid="+this.SiteID+"&spaceid="+this.ADID+"&id="+this.PosID;
- document.body.appendChild(new_element);
- }
- function showADContent() {
- var content = this.ADContent;
- var str = "";
- var AD = eval('('+content+')');
- if (this.ADType == "images") {
- if (AD.Images[0].imgADLinkUrl) str += "<a href='"+this.URL+'&a=poster_click&sitespaceid='+this.SiteID+"&id="+this.ADID+"&url="+AD.Images[0].imgADLinkUrl+"' target='_blank'>";
- str += "<img title='"+AD.Images[0].imgADAlt+"' src='"+this.UploadFilePath+AD.Images[0].ImgPath+"' width='"+this.Width+"' height='"+this.Height+"' style='border:0px;'>";
- if (AD.Images[0].imgADLinkUrl) str += "</a>";
- }else if(this.ADType == "flash"){
- str += "<object classid='clsid:D27CDB6E-AE6D-11cf-96B8-444553540000' width='"+this.Width+"' height='"+this.Height+"' id='FlashAD_"+this.ADID+"' codebase='http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0'>";
- str += "<param name='movie' value='"+this.UploadFilePath+AD.Images[0].ImgPath+"' />";
- str += "<param name='quality' value='autohigh' />";
- str += "<param name='wmode' value='opaque'/>";
- str += "<embed src='"+this.UploadFilePath+AD.Images[0].ImgPath+"' quality='autohigh' wmode='opaque' name='flashad' swliveconnect='TRUE' pluginspage='http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash' type='application/x-shockwave-flash' width='"+this.Width+"' height='"+this.Height+"'></embed>";
- str += "</object>";
- }
- str += "";
- document.write(str);
- }
- var cmsAD_1 = new PCMSAD('cmsAD_1');
- cmsAD_1.PosID = 1;
- cmsAD_1.ADID = 1;
- cmsAD_1.ADType = "images";
- cmsAD_1.ADName = "banner";
- cmsAD_1.ADContent = "{'Images':[{'imgADLinkUrl':'http%3A%2F%2Fwww.phpcms.cn','imgADAlt':'','ImgPath':'http://localhost/phpcms952/uploadfile/poster/2.png'}],'imgADLinkTarget':'New','Count':'1','showAlt':'Y'}";
- cmsAD_1.URL = "http://localhost/phpcms952/index.php?m=poster&c=index";
- cmsAD_1.SiteID = 1;
- cmsAD_1.Width = 430;
- cmsAD_1.Height = 63;
- cmsAD_1.UploadFilePath = '';
- cmsAD_1.ShowAD();
- var isIE=!!window.ActiveXObject;
- if (isIE){
- if (document.readyState=="complete"){
- cmsAD_1.Stat();
- } else {
- document.onreadystatechange=function(){
- if(document.readyState=="complete") cmsAD_1.Stat();
- }
- }
- } else {
- cmsAD_1.Stat();
- }
这是一个类型为banner的广告模型,我们拿出它的模版文件:
- function PCMSAD(PID) {
- this.ID = PID;
- this.PosID = 0;
- this.ADID = 0;
- this.ADType = "";
- this.ADName = "";
- this.ADContent = "";
- this.PaddingLeft = 0;
- this.PaddingTop = 0;
- this.Wspaceidth = 0;
- this.Height = 0;
- this.IsHitCount = "N";
- this.UploadFilePath = "";
- this.URL = "";
- this.SiteID = 0;
- this.ShowAD = showADContent;
- this.Stat = statAD;
- }
- function statAD() {
- var new_element = document.createElement("script");
- new_element.type = "text/javascript";
- new_element.src="{APP_PATH}index.php?m=poster&c=index&a=show&siteid="+this.SiteID+"&spaceid="+this.ADID+"&id="+this.PosID;
- document.body.appendChild(new_element);
- }
- function showADContent() {
- var content = this.ADContent;
- var str = "";
- var AD = eval('('+content+')');
- if (this.ADType == "images") {
- if (AD.Images[0].imgADLinkUrl) str += "<a href='"+this.URL+'&a=poster_click&sitespaceid='+this.SiteID+"&id="+this.ADID+"&url="+AD.Images[0].imgADLinkUrl+"' target='_blank'>";
- str += "<img title='"+AD.Images[0].imgADAlt+"' src='"+this.UploadFilePath+AD.Images[0].ImgPath+"' width='"+this.Width+"' height='"+this.Height+"' style='border:0px;'>";
- if (AD.Images[0].imgADLinkUrl) str += "</a>";
- }else if(this.ADType == "flash"){
- str += "<object classid='clsid:D27CDB6E-AE6D-11cf-96B8-444553540000' width='"+this.Width+"' height='"+this.Height+"' id='FlashAD_"+this.ADID+"' codebase='http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0'>";
- str += "<param name='movie' value='"+this.UploadFilePath+AD.Images[0].ImgPath+"' />";
- str += "<param name='quality' value='autohigh' />";
- str += "<param name='wmode' value='opaque'/>";
- str += "<embed src='"+this.UploadFilePath+AD.Images[0].ImgPath+"' quality='autohigh' wmode='opaque' name='flashad' swliveconnect='TRUE' pluginspage='http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash' type='application/x-shockwave-flash' width='"+this.Width+"' height='"+this.Height+"'></embed>";
- str += "</object>";
- }
- str += "";
- document.write(str);
- }
- var cmsAD_{$spaceid} = new PCMSAD('cmsAD_{$spaceid}');
- cmsAD_{$spaceid}.PosID = {$spaceid};
- cmsAD_{$spaceid}.ADID = {$p_id};
- cmsAD_{$spaceid}.ADType = "{$p_type}";
- cmsAD_{$spaceid}.ADName = "{$p_name}";
- cmsAD_{$spaceid}.ADContent = "{'Images':[{'imgADLinkUrl':'{urlencode($p_setting[1]['linkurl'])}','imgADAlt':'{$p_setting[1]['alt']}','ImgPath':'<?php echo $p_type=='images' ? $p_setting[1]['imageurl'] : $p_setting[1]['flashurl'];?>'}],'imgADLinkTarget':'New','Count':'1','showAlt':'Y'}";
- cmsAD_{$spaceid}.URL = "{APP_PATH}index.php?m=poster&c=index";
- cmsAD_{$spaceid}.SiteID = {$siteid};
- cmsAD_{$spaceid}.Width = {$width};
- cmsAD_{$spaceid}.Height = {$height};
- cmsAD_{$spaceid}.UploadFilePath = '';
- cmsAD_{$spaceid}.ShowAD();
- var isIE=!!window.ActiveXObject;
- if (isIE){
- if (document.readyState=="complete"){
- cmsAD_{$spaceid}.Stat();
- } else {
- document.onreadystatechange=function(){
- if(document.readyState=="complete") cmsAD_{$spaceid}.Stat();
- }
- }
- } else {
- cmsAD_{$spaceid}.Stat();
- }
这样,给我们的认识就是,模版上的变量被替换了,并重新生成了js文件,放到了相应的文件夹,代码如下:
- <?php
- /**
- *
- * @param 广告生成js类
- */
- defined('IN_PHPCMS') or exit('No permission resources.');
- class html {
- private $db, $s_db, $queue;
- /**
- *
- * 类的构造函数读取了三个模型作为了类的内部变量
- * 广告位,广告,队列
- *
- * 这里提一下队列模型
- *
- * phpcms的队列模型,就是在任务太多的情况下
- * 将其存入名为queue的表中
- * 处理完一个删除一个
- * 可以理解为排队机制
- *
- */
- public function __construct() {
- $this->s_db = pc_base::load_model('poster_space_model');
- $this->db = pc_base::load_model('poster_model');
- $this->queue = pc_base::load_model('queue_model');
- }
- /**
- * 生成广告js文件
- * @param intval $id 广告版位ID
- * @return boolen 成功返回true
- */
- public function create_js($id = 0) {
- $id = intval($id);
- if (!$id) {
- $this->msg = L('no_create_js');
- return false;
- }
- $siteid = get_siteid();
- /**
- *
- * 这里的生成js文件就是单一的
- * 在上文提到的调用中循环采用了这个
- * 所以下面拿到的是当前站点id下的广告位的数据
- *
- */
- $r = $this->s_db->get_one(array('siteid' => $siteid, 'spaceid' => $id));
- $now = SYS_TIME; //将系统时间写入变量
- if ($r['setting'])
- $space_setting = string2array($r['setting']); //如果存在广告位的设置,就转化成数组,其实肯定存在的
- if ($r['type'] == 'code')
- return true; //代码类型的就直接返回了,因为不用生产js文件
- $poster_template = getcache('poster_template_' . $siteid, 'commons'); //读取了广告模版设置的缓存
- /**
- *
- * 下面进行一个判断
- *
- * 判断广告位的类型配置是不是存在option一项
- *
- *
- */
- if ($poster_template[$r['type']]['option']) {
- /**
- * 如果存在
- * 获取
- * 当前站点 当前广告位 能使用 设置时间在当前时间内 , 按照 人为排序正序 id倒序
- * 的所有广告
- *
- * 并且打散数组
- */
- $where = "`spaceid`='" . $id . "' AND `siteid`='" . $siteid . "' AND `disabled`=0 AND `startdate`<='" . $now . "' AND (`enddate`>='" . $now . "' OR `enddate`=0) ";
- $pinfo = $this->db->select($where, '*', '', '`listorder` ASC, `id` DESC');
- if (is_array($pinfo) && !emptyempty($pinfo)) {
- foreach ($pinfo as $k => $rs) {
- if ($rs['setting']) {
- $rs['setting'] = string2array($rs['setting']);
- $pinfo[$k] = $rs;
- } else {
- unset($pinfo[$k]);
- }
- }
- extract($r);
- } else {
- return true;
- }
- } else {
- /**
- * 如果不存在
- *
- * 取出数据的方式和上面一致
- *
- * 但是在打散的数组的每个变量前面都加上了 P
- *
- * 所以对于该广告模块来说,充分使用了extract这个函数
- *
- * 有兴趣的同学可以研究下
- *
- */
- $where = " `spaceid`='" . $id . "' AND `siteid`='" . $siteid . "' AND `disabled`=0 AND `startdate`<='" . $now . "' AND (`enddate`>='" . $now . "' OR `enddate`=0)";
- $pinfo = $this->db->get_one($where, '*', '`listorder` ASC, `id` DESC');
- if (is_array($pinfo) && $pinfo['setting']) {
- $pinfo['setting'] = string2array($pinfo['setting']);
- }
- extract($r);
- if (!is_array($pinfo) || emptyempty($pinfo))
- return true;
- extract($pinfo, EXTR_PREFIX_SAME, 'p');
- }
- $file = CACHE_PATH . $path;
- /**
- *
- * 这里用的ob函数进行输出
- *
- * 优点:不会因为php文件提前输出header而出错,又可以及时刷新缓存区而不浪费资源
- */
- ob_start();
- /**
- *
- * 这里在文件输出流中引入了广告模版文件
- * 而在原始的模版文件中存在着大量的php变量
- * 这些变量和上面被打散的数组中的变量是一致的
- * 也就是说在这一步
- * 变量被巧妙的替换了
- * 很自然的转化成了js文件
- */
- include template('poster', $type);
- $data = ob_get_contents();
- /**
- *
- * 将合并后的文件内容存进变量后清空了ob文件流
- */
- ob_end_clean();
- /**
- *
- * 最后根据路径写入文件
- */
- $strlen = pc_base::load_config('system', 'lock_ex') ? file_put_contents($file, $data, LOCK_EX) : file_put_contents($file, $data);
- @chmod($file, 0777);
- return true;
- }
- }
- ?>
四、总结
根据上面的图文注释,小伙伴们都可以清楚的知道phpcms的广告是怎么产生的了吧.
配置文件+模版文件+数据库中具体数据=js广告文件
五、后记
用了一晚上的时间,又重新分析了一下phpcms,这个功能原来在大连实训的时候写过,也是参考phpcms的,这回也算是复习吧.
温故知新,回头再看phpcms,还是有很多值得学习的,看来有空还要研究下它的队列机制了.
Tags: PHPCMS广告模块 PHPCMS广告生成
推荐文章
热门文章
最新评论文章
- 写给考虑创业的年轻程序员(10)
- PHP新手上路(一)(7)
- 惹恼程序员的十件事(5)
- PHP邮件发送例子,已测试成功(5)
- 致初学者:PHP比ASP优秀的七个理由(4)
- PHP会被淘汰吗?(4)
- PHP新手上路(四)(4)
- 如何去学习PHP?(2)
- 简单入门级php分页代码(2)
- php中邮箱email 电话等格式的验证(2)