Home  >  Article  >  Backend Development  >  Detailed explanation of dedecms code is very comprehensive_PHP tutorial

Detailed explanation of dedecms code is very comprehensive_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:39:17997browse


dedecms code research (1) opening
dedecms I believe everyone must know this cms system, which has relatively powerful functions, relatively complete content publishing, and static content system, and it has its own unique tag system and template system. The template system is also something that other cms systems are difficult to imitate. This
thing still requires some development skills and skills.
This series of articles will study the dedecms system and dig into it to see what good stuff there is.
It is recommended that you first understand the functions of dedecms. Use it yourself first to get a general understanding of the system's functions.
This article will first take you through the code and functional architecture of dedecms.
In fact, dedecms has no application architecture model to speak of in terms of architecture. It is just the simplest PHP application. Access different PHP files and
manage different system functions. Looking at the directory, everyone can guess what each directory is used for. The include directory contains some common functions and classes of the system, the
plus directory contains plug-ins, the templates directory contains templates, and the dede directory is the management backend directory.
Let’s open the include directory and see what good stuff is in it.
calendar, a js for selecting time,
captcha, a verification code, or an open source component,
code is meaningless, and the text for turning pages
data contains some resources used by the system, such as sounds and word segmentation. Libraries, fonts, pictures, etc.
dialog is probably the content of some AJAX pop-up windows. I will talk about some separate functional functions in
inc later when I encounter them. I will study why the
payment payment interface
taglib is placed separately. Good thing, it is where dedecms tags are stored. Open it and see that there is a The pile of files seems to be the dedecms template tag
tagged
tpllib template library? I can’t figure it out for now, but I hope I can figure it out with further research.
The files in other include directories are probably some of the most basic functional files, such as commonly used functions, template systems and the like. Use
to talk about it later.
Open the dede directory (which is the management directory of dedecms) and take a look. Oh wow, there are quite a lot of things in it. You can tell by looking at the names. They are all various
functional management files. One file per function. Made with the most original website development model. Let’s call it efficient. After all, PHPwind
and discuz are also developed using similar methods.
The whole program is basically about these things. I won’t talk about the functions. It is recommended that friends who are not familiar with dedecms take a look at the background management function of dedecms. This will help us better understand and analyze it.
In addition, our code analysis this time mainly analyzes its page generation, display, template processing, and label processing parts. This
system is also something dedecms is quite proud of. After that, I will also analyze some functional codes that I think are cool.
Finally, I hope everyone can gain something from this code analysis journey.
Sourcejoy’s dedecms code research (2) Starting from index
Let’s continue now. Today we are mainly talking about the entry code of dedecms.
2
First open index.php and see what’s inside. Open index.php in the root directory. Well, what comes into view is an if statement.
Check if /data/common.inc.php exists. If it does not exist, jump to the installation interface.
Let’s go to /data/ and take a look at this directory and common.inc.php.
Open /data/, there are many directories and files in it, including uploaded temporary directories, template caches, compressed temporary directories, cache files that save
's system configuration information in various databases, and other things. One word is messed up~
Okay, let’s open common.inc.php. Oh, it turns out that it just saves the variables related to the database connection. To put it bluntly, it is the database connection configuration file of dedecms
, which is probably generated after the system is installed. So /index.php will check if it exists.
Let’s go back to /index.php and continue to look down. The second if statement determines whether the GET request "upcache" exists. If it exists, update the first
page cache. If it does not exist, jump directly to index with 301 .html, which is the static homepage of dedecms (remember, the front-end pages of dedecms are all static pages generated by the system).
Think about what we should study next? Yes, it is a small piece of code that updates the homepage cache when the GET request "upcache" exists.
The code is as follows:
require_once (dirname(__FILE__) . "/include/common.inc.php");
require_once DEDEINC."/arc.partview.class.php";
$GLOBALS ['_arclistEnv'] = 'index';
$row = $dsql->GetOne("Select * From `dede_homepageset`");
$row['templet'] = MfTemplet($row[' templet']);
$pv = new PartView();
$pv->SetTemplet($cfg_basedir . $cfg_templets_dir . "/" . $row['templet']);
$pv ->SaveToHtml(dirname(__FILE__).'/index.html');
include(dirname(__FILE__).'/index.html');
exit();
Load /include first /common.inc.php, it is estimated to be some commonly used functions and files that load other system functions and classes.
Next, DEDEINC."/arc.partview.class.php" is loaded again. Pay attention to the constant DEDEINC. We know that after the previous inspection
Observing the directory structure of dedecms, arc.partview.class.php is in the /include directory, but loading /include/common.inc.php does not use the
DEDEINC constant. This shows that common.inc.php is defined in The constant DEDEINC is removed, so it can be used later, which also proves
that the general function of common.inc.php is the basic part of system operation, which is all in it. We are not in a hurry to enter common.inc.php and continue to read the updated cache code of
index.php.
In the third sentence, a global variable is set: $GLOBALS['_arclistEnv'] = 'index';
In the fourth line, all records of a table 'dede_homepageset' are obtained. Of course, if you look at the database, it There is no
"dede_homepageset" table. We judge that the function related to database operation replaces the first half of the table name with the table name prefix. This
is not important anymore. What is important is that through this sentence, we can obtain After knowing the relevant configuration information of the homepage, we open the dede_homepageset
table in the database. There is only one record and two fields. We basically guessed it. One is the name of the homepage template and the other is the location of the generated static file.
Come back and continue to analyze the previous code
$row['templet'] = MfTemplet($row['templet']);
It seems that $row['templet'] has been modified in some way through the MfTemplet function Convert. We note the MfTemplet function for later exploration.
The next step is to create a new PartView class. Looking at the name, we know the purpose of loading arc.partview.class.php earlier.
As for the function of this PartView class, let’s continue to look at the code. After reading the following two lines, I think everyone should understand
$pv->SetTemplet($cfg_basedir . $cfg_templets_dir . "/" . $row['templet']);
3
$ pv->SaveToHtml(dirname(__FILE__).'/index.html');
Create a view object (an instance of the PartView class, let’s call it a view object), set the template, and use the SaveToHtml method to save the most
The generated page is written to the specified location.
After the homepage is generated, the next step is to display the generated static files in the form of include, and then exit to interrupt page parsing.
At this point, /index.php has been analyzed.
It first passes /data/common.inc.php to determine whether dedecms is installed or whether the database configuration information is defined, so as to
lay the foundation for subsequent operations.
Then determine whether there is a GET request "upcache", if so, load /include/common.inc.php to initialize the system, and then call the relevant methods of the
partview class to generate a static home page file, and finally display it.
Looking at it from this point of view, dedecms doesn’t have many secrets~
However, our article also leaves a few questions:
1) /include/common.inc.php is loaded, which What work was done?
2) What does /include/arc.partview.class.php do? What are the many files starting with arc under /include/?
3) What exactly does the MfTemplet function do to the string of template file path?
4) What are the secrets of the related methods of the partview class?
With these questions, we will end this article, and the following articles will reveal these mysteries one by one.
Sourcejoy’s dedecms code research (3) Confusion of partview
Last time, we learned a lot of information from dedecms’ index.php file and also raised some questions. This article starts with the previous questions,
Let’s continue our dedecms journey.
Let’s first review what we summarized in the previous study of the index.php file.
First load common.inc.php, then organize the template, generate a static page and jump to the static page.
Next, let’s take a look at what’s in common.inc.php. Open /include/common.inc.php and the comments in it have been made clearer. Let’s talk about structure roughly.
First define a bunch of constants. Then we need to take some security measures and make some settings for the PHP system environment. The comments in the code have been clearly written
.
The next step is to include the system configuration parameter file of dedecms: require_once(DEDEDATA."/config.cache.inc.php");
Looking at the file name, we guess that this configuration file may be the configuration in the database Caching of information.
Next, the database configuration information file is loaded: require_once(DEDEDATA.'/common.inc.php');
This file is not a cache generated based on the information in the database, but generated when dedecms is installed. In the previous article, we said that starting from the
index.php file, checking whether dedecms is installed is to see whether this file exists.
Next, we organized many directories, such as the site root directory, template directory, plug-in directory, data directory, etc., and also organized many variables
. Finally, the database operation class dedesql.class.php and the common function file common.func.php
4
were loaded. Well, the mystery of common.inc.php was revealed. There is nothing interesting in it, it is all the most basic. something.
Next we have to take a look at arc.partview.class.php, which is the key to dedecms
Loaded channelunit.class.php, typelink.class.php, ftp.class.php
The following is the definition of the partview class
Because the SetTemplet method and SaveToHtml method of the partview class are used in index.php, in order to make it easier to delve deeper, we will start with these two methods.
Let’s take a look at the constructor of partview first.
Created an instance of the DedeTagParse class. The name is the tag parsing class. Then set several parameters.
Next, create a new TypeLink class and set a bunch of parameters. It was very confusing.
Have a look at SetTemplet. Ah, this is quite simple.
First, use the LoadTemplet method of the DedeTagParse class to load the template.
Next, set some elements of the Fields array
Finally, call the ParseTemplet method.
The ParseTemplet method gets a bunch of elements of the $GLOBALS array, and then calls the MakeOneTag function. It’s confusing
Let’s take a look at the SaveToHtml method. The first step is to create a directory, and finally use the SaveTo method of DedeTagParse to save it to a file.
Uh~ It’s not strong enough.
I can only look back and think about what I saw~
Well, in order to generate the home page, I made a very confusing partview class, and then called the seemingly omnipotent DedeTagParse method to parse the module
Board, generate static files.
That’s all.
There are other functions and classes mixed in, but no matter what, this DedeTagParse is the focus, and I will focus on analyzing it next time.
That’s it for today, it’s so messy and not organized at all~
Sourcejoy’s dedecms code research (4) Before continuing to wander partview
, we were like falling into a swamp. Endless variables, array elements, and inexplicable things are placed in front of us. Today, we continue to move forward arduously, trying to find a way out of the quagmire of partview.
In the previous article, we randomly analyzed the partview class and had no idea what it was doing. There were a bunch of variables in it. The clearest thing we know is that a few key places to generate the homepage
call the DedeTagParse class. LoadTemplet method and SaveTo method. The file header defined by the partview class contains several files. Let's
avoid partview and take a look at these included files first.
require_once(DEDEINC.'/channelunit.class.php');
require_once(DEDEINC.'/typelink.class.php');
require_once(DEDEINC.'/ftp.class.php') ;
5
ftp.class.php, needless to say, is the ftp related operation class. When we looked at the partview code before, we learned that when generating static files, ftp
related methods were used , it seems that you can write files remotely. As for how to operate FTP, it actually encapsulates the related functions starting with ftp in the PHP function library. The code is very simple, so I won’t go into details.
typelink.class.php, we have also seen it in the partview code, open it and take a look. After briefly looking at the code, there are things related to the type of link. Each
method claims a link html string of the specified type.
Actually, I have a vague feeling that in dedecms, type refers to the column. I don’t know if this is the case.
Let’s take a look at channelunit.class.php again.
Inside is the definition of the ChannelUnit class, and we found that this ChannelUnit class has not been used. So don’t go see him yet. We noticed that two files were also added:
require_once(DEDEINC."/dedetag.class.php");
require_once(DEDEINC."/channelunit.func.php") ;
dedetag.class.php, open it and take a look, well, it’s very complicated, but we found that the DedeTagParse class used to parse templates and generate files is in it, haha, remember it first,
will study it slowly later.
What functions are in channelunit.func.php? Open it and see, well, there are a bunch of variables and several functions for obtaining this kind of information. During our browsing, we found two
functions: MfTemplet and MakeOneTag
We know that they are used in index.php MfTemplet function, go back and open index.php to see how to call it:
$row['templet'] = MfTemplet($row['templet']);
We have learned before, $row['templet '] saves the value default/index.htm, which is the template file path. Next, let’s take a look at what the MfTemplet function does
.
//Template directory rules
function MfTemplet($tmpdir)
{
$tmpdir = str_replace("{style}",$GLOBALS['cfg_df_style'],$tmpdir);
$tmpdir = ereg_replace("/{1,}","/",$tmpdir);
return $tmpdir;
}
The comment says "template directory rules", look at the code again , Oh, I seem to understand a little bit, that is to replace {style} in the template path with the value in the global variable
$GLOBALS['cfg_df_style']. It probably has something to do with using different template sets. If it is not of great significance, we will not continue to study it.
Let’s look at another function MakeOneTag, which is in the ParseTemplet method of the partview class. This method parses the template based on the name, and most of the code of the method
is dealing with variables. It’s hard to understand what it does. The last sentence calls the MakeOneTag function. It seems that this function is mainly used to parse templates. Use as follows:
MakeOneTag($this->dtp,$this);
The first parameter is the strength of the DedeTagParse class, and the second parameter is the handle of the partview class instance.
Let’s take a look at what this function in channelunit.func.php does.
Well, I can only take a rough look at it, because we don’t know many things clearly, which makes me depressed.
This traverses all files with lib suffix under /include/taglib/, adds the file path to the array, and then traverses the CTag of the DedeTagParse class.
6
Since we have not studied the DedeTagParse class , so I don’t understand this yet, but it can be considered a small progress. It seems that I have to go back to partview and take a look again.
The constructor is nothing special, it just creates an instance of the DedeTagParse class and makes some settings. We know that index.php executes the SetTemplet method after creating the partview instance. Let's take a look at the SetTemplet method again.
The LoadTemplet method of the DedeTagParse class instance is called here. It seems that we have to start from here to peel off the cocoon.
I can’t continue the analysis~
Leave a few questions for next time.
1) Let’s talk about the LoadTemplet method of DedeTagParse class.
2) What on earth is MakeOneTag doing?
It seems that the only way to go further is to thoroughly understand the LoadTemplet method of the DedeTagParse class. I am still confused at the moment.
Sourcejoy’s dedecms code research (5) Starting from DedeTagParse
Previously, we have been on the periphery of dedecms, confused by various global variables and various calls. We grabbed a key clue, the DedeTagParse class, and studied
Understand it and you can figure out a lot of things.
Look at this NB’s DedeTagParse class.
Well, let’s look at the constructor first. It’s nothing special, it just sets a bunch of initialization parameters.
Let’s look for the LoadTemplet method next.
After finding it, we found that the LoadTemplet method actually points to the LoadTemplate method. I am speechless. Is the author's English so poor?
Look at the LoadTemplate method.
First use the SetDefault method to set several initial variables:
$this->SourceString = '';
$this->CTags = '';
$this->Count =-1;
Then determine whether the template file exists. Then assign a value to $this->SourceString according to different situations, and call the $this->ParseTemplet(); method.
It can be seen from this code that the author's development skills need to be improved. It is already 5.6, and the code refactoring is still so bad. Alas~ why can't you put $this->ParseTemplet();
in if What about outside?
When the file does not exist, it is very simple to put the sentence "The file does not exist" in $this->SourceString, and then call $this->ParseTemplet();.
When the file exists, it is also very simple. fgets reads the file content (trouble, why not use file_get_contents), and then, another if, returns the value through
$this->LoadCache($filename) Determine whether there is a cache. If true is returned, it means that the cached template has been read, and an empty string is returned (how can this be done like this? The return value is too irresponsible). If false is returned, $this-> is called. ;ParseTemplet();Reparse the template.
LoadTemplate is roughly the same. It is nothing more than reading the content of the template file and then checking whether there is a cache. If there is a cache, it will not parse the template. If not, it will parse the template. That's it.
Let’s take a look at the $this->LoadCache method next. Find the part where the method is defined, wow, there’s a lot of code.
7
First, use $this->IsCache to determine whether caching is allowed (this attribute is set when the DedeTagParse class is instantiated, and is related to the system configuration of dedecms
whether to add the template cache parameter$ cfg_tplcache is related, this is reflected in the constructor of the DedeTagParse class. Since dedecms is installed, the default system
configuration is true, so the default here is true), if it is false, $this->LoadCache Return false instead of continuing down. In the LoadTemplate
method, the template will be parsed based on this return value.
After passing $this->IsCache, the program continues. The following is to find the cache corresponding to the current template file.
The file cache of dedecms is a bit special. We find the template cache directory (data/tplcache). If we observe it, we will find that there are many files with the same names. If we look carefully
we can find some patterns. Let’s verify it from the code.
As mentioned above, in the LoadCache method, we have passed the $this->IsCache level, and then we are looking for the template file. Let’s take a look at the following code:
$cdir = dirname($filename);
$cachedir = DEDEROOT.$cfg_tplcache_dir;
$ckfile = str_replace($cdir,'',$filename).substr(md5($filename),0,16).'.inc';
$ckfullfile = $cachedir.'/'.$ckfile;
$ckfullfile_t = $cachedir.'/'.$ckfile.'.txt';
The first three sentences are to spell the name of the cache file. The method is to take it without Then use md5 to hash the template file name of the directory, and then take the first 16 characters of the hashed string and add the "inc" suffix after
.
Sentence 4 is to obtain the complete cache file name.
The fifth distance seems to be another file name, which is to add a suffix ".txt" after the cache file name.
The above gives us two file names, but we don’t know what the second file name is used for. Yes, let’s continue reading the code below.
$this->CacheFile = $ckfullfile;
$this->TempMkTime = filemtime($filename);
if(!file_exists($ckfullfile)||!file_exists($ckfullfile_t))
{
return false;
}
The first sentence specifies the cache file of the current template
The second sentence reads the last modification time of the file and sets a time attribute. It is not clear yet.
The next if statement is that if the two cache files of the template (the two files combined above) cannot be found, return false and let the LoadTemplate method parse the template.
We assume that the cache files of the template are available and continue to look at the code. The following code snippet is commented, which is to detect the last update time of the template. The code is very simple. It is to open the txt file specified by the $ckfullfile_t variable we mentioned earlier, read the content, and then compare the content with the cache modification time. It turns out that The .txt file is used to save the save time of cached files. If the time is inconsistent, return false and let the LoadTemplate method parse the template.
We assume the cache is valid and can continue.
Cache files will be included if the cache is valid.
This section needs to be analyzed in detail based on the cache file, so let’s assume that the index.htm template is loaded here. Find the beginning of index.htm in tplcache,
8
with the suffix inc File, open.
Here we excerpt a part:
$z[0]=Array("global","",236,264);
$z[0][4]['name']="cfg_soft_lang";
$z[1]=Array("global","",277,303);
$z[1][4]['name']="cfg_webname";
$z[2] =Array("global","",347,377);
$z[2][4]['name']="cfg_description";
$z[3]=Array("global"," ",414,441);
$z[3][4]['name']="cfg_keywords";
...
Go back to our LoadCache method.
As mentioned earlier, the template cache file is included. Then, the following if statement determines whether the information array "$z" in the cache file is normal. If it is normal, perform a
foreach loop. This foreach is very important. . Let's take a look at the code.
foreach($z as $k=>$v){
$this->Count++;
$ctag = new DedeTAg();
$ctag->CAttribute = new DedeAttribute ();
$ctag->IsReplace = FALSE;
$ctag->TagName = $v[0];
$ctag->InnerText = $v[1];
$ctag->StartPos = $v[2];
$ctag->EndPos = $v[3];
$ctag->TagValue = '';
$ctag-> TagID = $k;
if(isset($v[4]) && is_array($v[4])){
$i = 0;
foreach($v[4] as $k =>$v){
$ctag->CAttribute->Count++;
$ctag->CAttribute->Items[$k]=$v;
}
}
$this->CTags[$this->Count] = $ctag;
}
This is a traversal of the cache information array, and then each element of the $z array generates a DedeTAg object. And assign some information of the $z array element to the DedeTAg object
. After looking at this source code, we found that in the $z array element, 0 is the tag name (TagName), 1 is the internal text (InnerText), 2 is the start position (StartPos), and
3 is the end position (EndPos). The tagID of the new DedeTag object is the array index.
There is also a loop here, which is the fourth sub-element of each element of the $z array. Then, assign the relevant values ​​​​to the DedeAttribute object of the current DedeTAg object
9
.
Seeing this, we seem to understand something.
1) What is stored in tplcache is not the parsed template, but a bunch of information arrays.
2) The information array stores the information of all tags contained in a template page.
3) The above loop actually reads and writes the tag information in the cache to the DedeTAg object, and then saves it to the CTags array of the current DedeParse class. At the current time, the current instance of DedeParse gets the template content (in $this->SourceString), all tag information on the template (in $this->CTags
).
Okay, after the above operations, that’s it for the LoadCache method. The cache reading is completed, so you can safely return to the LoadTemplate method to continue the analysis
.
Sourcejoy’s dedecms code research (6) ParseTemplet algorithm analysis
I started to get sick three days after the National Day holiday. After I recovered, I started to be crazy busy. I was dizzy. After yesterday’s efforts, I can finally continue to update. Today we are talking about the most critical
thing of dedecms, template analysis.
Let’s take a look at the dedecms tag first, and you will have an idea:
{dede:arclist row=10 orderby=pubdate type='image.' imgwidth='143' imgheight='106'}
< ;li>[field:image/][field:title/]
{/dede:arclist}
We can further analyze by referring to the tags above.
It is assumed that you already understand the tag form, tag format, and tag types of dedecms.
Let’s start the analysis below
Let’s first look at some of the most basic variables initialized before the method:
1) Tag start symbol and end symbol. Such as: "{" and "}"
$TagStartWord = $this->TagStartWord;
$TagEndWord = $this->TagEndWord;
2) Set temporary variables to temporarily store the starting and ending positions of the found new tags in the template.
$sPos = 0; $ePos = 0;
3) Set the complete label start string and end string. For example: "{dede:" in the form
$FullTagStartWord = $TagStartWord.$this->NameSpace.":";
$sTagEndWord = $TagStartWord."/".$this->NameSpace. ":";
$eTagEndWord = "/".$TagEndWord;
It is worth noting here that there are two types of end parts, one is a single structure tag similar to {aa:ff /}, and the other is Similar to {aa:fff}{/aa:fff} conforms to the structure tag
4) Get the actual string ({dede:) length of the tag and the length of the entire template
$tsLen = strlen($FullTagStartWord);
$sourceLen=strlen($this->SourceString);
The above is the initial variable setting part.
The next step is a small judgment. If the length of the entire template is not greater than the length of the label's starting string plus 3, exit.
if( $sourceLen <= ($tsLen + 3) ){
return;
}
Why add 3 (that is, the template length should be at least the length of the label starting string plus 4 )Woolen cloth?
10
Let’s look at the shortest tag we can write:
{dede:a/}
After the colon is the shortest possible string, which is 3, so if it is less than 3 here, Even the most basic label cannot be complete, so to make this judgment,
is equal to or equal to, I personally think it is unnecessary.
Okay, continue reading the following two sentences:
$cAtt = new DedeAttributeParse();
$cAtt->charToLow = $this->CharToLow;
Created a DedeAttributeParse class, and The CharToLow attribute is set. The name of this class should be the label attribute analysis class. charToLow is whether to automatically convert the
string into lowercase.
The next step is a long for loop, which traverses each character of the template string to analyze and extract the tags in the template.
for($i=0; $i < $sourceLen; $i++)
Let’s take a look at how this for loop is analyzed
First define a temporary variable to store the current finding The name of the tag
$tTagName = '';
The following is a judgment. The comments are clearly written, but we can’t understand it yet, so we just need to know that there is such a judgment first
//If Without this judgment, the two connected tags will not be recognized
if($i-1 >= 0){
$ss = $i-1;
}else{
$ss = 0;
}
sets a variable $ss, just pay attention to it later.
The following is to find the tag
$sPos = strpos($this->SourceString,$FullTagStartWord,$ss);
$isTag = $sPos;
Find the template string from $ Starting from the position specified by ss, the first tag header position similar to "{dede:" is set, and the $isTag variable is set to the return value of strpos.
This is a lazy way of writing, and it should be clearly stated that the tag has been found. , is true, not any character.
We see that $ss is used here, which is used to set the starting position of the search.
Let’s continue reading
The following if statement seems to be a supplement to the case where the first character starts with a label?
I don’t understand. I could have found it already. What does this sentence mean? It’s superfluous, there must be a better way to do this. No more words to say.
The if below means that if the tag is not found, it will not loop and there will be no explanation.
Come down again, a sub-loop
for($j=($sPos+$tsLen);$j<($sPos+$tsLen+$this->TagMaxLen);$j++)
$tsLen Before we As mentioned, it is the length of the tag header (similar to {dede:)
. Then the explanation of this for is to traverse all the characters from the next character of the tag header to the end of the maximum length of the tag. It seems to be looking for the tag name.
Look inside the for loop again. It’s just a few simple sentences to find out the name of the tag. How do you find it out?
if($j>($sourceLen-1)){
break;
}else if( ereg("[/ trn]",$this->SourceString[$j]) || $this->SourceString[$j] == $this->TagEndWord ){
break;
}else{
$tTagName .= $this->SourceString[$j];
11 http://www.pprar.com
}
The if statement in this for, the name ends in two cases, one is that the character position is one position after the word of the template, the other is Spaces, line breaks, tab characters, /
, etc., or tag end characters (such as: "}")
are found. Through this for loop, the name of the tag is found and stored in the variable $tTagName.
The following is an extremely long if statement to determine whether the $tTagName variable is empty. If it is empty, jump out of the loop (the tag is wrong), but before jumping out, you also
set $i. What's the use? can't read.
The next key point is to find the tag name.
First set a few variables
$i = $sPos+$tsLen;
$endPos = -1;
$fullTagEndWordThis = $sTagEndWord.$tTagName.$TagEndWord;
Put the loop template characters The string pointer $i jumps to the beginning of the tag name. Then set the variable $endPos to -1 to combine a tag end character ({/dede:xxx})
The next step is to find three positions: $eTagEndWord (/}), $FullTagStartWord ({dede:), $fullTagEndWordThis({/dede:xxx})
$e1 = strpos($this->SourceString,$eTagEndWord, $i);
$e2 = strpos($this->SourceString,$FullTagStartWord, $i);
$e3 = strpos($ this->SourceString,$fullTagEndWordThis,$i);
$e1 is the position where the first "/}" appears after the tag name is found, $e2 is the position where the first "{dede:" appears, $e3 is the
position where the first {/dede:xxx} appears. Note here that when getting the $e3 value, $fullTagEndWordThis is the ending string named after the currently found tag.
The following sentences unify the values ​​​​of $e1 $e2 $e3, so that if the label string you are looking for is found, the position of these three variables will be saved, and if it cannot be found, -1 will be saved
$e1 = trim( $e1); $e2 = trim($e2); $e3 = trim($e3);
$e1 = ($e1=='' ? '-1' : $e1);
$e2 = ($e2=='' ? '-1' : $e2);
$e3 = ($e3=='' ? '-1' : $e3);
The next step will be based on this The three values ​​have undergone some processing. What to deal with? Let's take a look at this code first:
//not found '{/tag:'
if($e3==-1) {
$endPos = $e1;
$elen = $endPos + strlen($eTagEndWord);
}
//not found '/}'
else if($e1==-1) {
$endPos = $e3;
$elen = $endPos + strlen($fullTagEndWordThis);
}
//found '/}' and found '{/dede:'
else{
//if '/}' more near '{dede:', '{/dede:' , end tag is '/}', else is '{/dede:'
if($e1 < $e2 && $e1 < $e3 ){
$endPos = $e1;
$elen = $endPos + strlen($eTagEndWord);
}else{
12
$endPos = $e3;
$elen = $ endPos + strlen($fullTagEndWordThis);
}
}
We know that there are two ways to end the dedecms tag, one is (/}), and the other is ({/dede: xxx}), other than that, there is no other choice. If there are no
these two endings, it can only mean one problem, the tags in the template are incomplete. This if statement makes an assumption that there must be one of two ways to end a tag.
The first branch of if, assuming $e3 is -1, that is, (/}) exists, so the tag end position variable $endPos is set to the value of variable $e1, and at this time,
The final end position of the label is known, which is the length of $endPos plus (/}).
The second branch of the if statement is similar to the first, except that it is assumed that ({/dede:xxx}) is found. The else part of the
if statement assumes that both are found (is this possible?), then further analysis is required. If (/}) this type of terminator appears in the position
of The starting position of each tag is earlier, and it is also earlier than the end character ({/dede:xxx}) of $e3, which means that the currently found (/}) is the end character
of the current tag; otherwise it must be ({/dede:xxx}) This kind of thing.
Through the variable settings of $e1 $e2 $e3 and an if statement, two variables are finally obtained: $endPos and $elen, the starting position
and the ending position of the current label end character.
The following is another if statement. It is very simple. It determines whether the current tag ends correctly by whether endPos is -1. If it does not end correctly, print a piece of text and then
exit the loop. Can this design be better? For example, replace the erroneous tag with an error message, or check the syntax for correctness
before doing template analysis to ensure faster template analysis.
Looking further down, two more variables are set.
$i = $elen;
$ePos = $endPos;
Since the label that the current loop is looking for is found, set the loop variable $i of the main loop for to the starting position of the next label.
Set the starting position of the end character of the current label $ePos.
The start position and end position of the current tag are determined. Next, we can analyze the attributes of the tag. Let’s continue.
$attStr = '';
$innerText = '';
$startInner = 0;
Three variables, we learned that there are two things inside the tag, one is the attribute string , and another is the content string. The $startInner variable indicates whether the content string starts with
(strange why a Boolean value is not used).
The following for loop begins to extract these strings, from the end of the tag name to the part before the end character.
for($j=($sPos+$tsLen);$j < $ePos;$j++)
Look at how the attribute string and content string are extracted in the loop.
if($startInner==0 && ($this->SourceString[$j]==$TagEndWord && $this->SourceString[$j-1]!="\") ){
$startInner=1;
continue;
}
if($startInner==0){
$attStr .= $this->SourceString[$j];
}else{
$innerText .= $this->SourceString[$j];
}
Well, two if statements are used. The first statement is used to determine whether the content string has started. The second if statement is judged based on the content string start indicator and reads the
13
content string and attribute string respectively.
Personally, I think it is faster to truncate using special identifiers.
There is another question here: how to determine whether the content string starts?
Let’s look at the first if
if($startInner==0 && ($this->SourceString[$j]==$TagEndWord && $this->SourceString[$j-1]! ="\") )
$startInner==0 This sentence is a filter. When reading the content string, this if will not be used. The key is the content in the brackets after &&.
If the current character is the tag end character $TagEndWord (}) and the character before the end character is not a backslash, the attribute part is over. If it is a backslash
, it means that it is some template content or the like. Yes.
Through the above for loop, we extracted the attributes and content of the current tag, and then started to analyze the attributes and content
$cAtt->SetSource($attStr);
if($cAtt ->cAttributes->GetTagName()!=''){
$this->Count++;
$CDTag = new DedeTag();
$CDTag->TagName = $cAtt- >cAttributes->GetTagName();
$CDTag->StartPos = $sPos;
$CDTag->EndPos = $i;
$CDTag->CAttribute = $cAtt-> ;cAttributes;
$CDTag->IsReplace = FALSE;
$CDTag->TagID = $this->Count;
$CDTag->InnerText = $innerText;
$this ->CTags[$this->Count] = $CDTag;
}
Analyze it through the attribute analysis class, then create a DedeTag tag class instance (that is, create a tag object), and then add the current tag All attributes are put into this
tag object.
Includes tag name, starting position, ending position, attribute array, internal string, etc.
Then, put this new tag object into the CTags array of the DedeTagParse class.
The analysis of such a tag is completed, and the outermost for loop is ended. It turns out that only one tag can be analyzed per loop, and the loop may be repeated
as many times as there are tags.
After the entire template analysis is completed, if caching is allowed, call the SaveCache method to save the tag information of the current template to a cache file or intermediate information file.
The template analysis is finished, so that all the information is available, and we can return to the LoadTemplate method to continue.
Sourcejoy’s dedecms code research (7) MakeOneTag
I’ve been too busy recently, so busy that I can’t update the website, and I haven’t read the book for a long time. If you have free time today, continue to finish the unfinished business. Today I will continue the previous article to talk about the static page generation process of dedecms
.
To briefly review, previously our homepage dynamic file index.php called arc.partview.class.php to initialize the homepage template. arc.partview.class.php called DedeTagParse through
SetTemplet. When setting the homepage template, The template is parsed and a cache or serialized file of the template is generated. Although the template is parsed
, this parsing only records the position of all tags in the template and their parameters, and does not fill in the data. It is estimated that these things will be done next.
Let’s go back to the SetTemplet method of partview and look down from the sentence $this->dtp->LoadTemplet($temp);.
14
The if statement below if($this->TypeID > 0) is to set the "breadcrumb navigation" and title of the current page. If you have used dedecms and are familiar with the operation of the dedecms system, you should know that the $this->Fields['title'] array element in this if is used in many places in the page template. What about the fields array.
After setting two variables, call ParseTemplet to parse the template.
This ParseTemplet is very simple. It has set up a lot of global variables. You can tell at a glance what channelid, topid and other things are used in the page. Friends who are interested
can study it in detail by themselves. We noticed that the last sentence of the ParseTemplet method, MakeOneTag($this->dtp,$this); because the previous things are
and have no substance, this should be very critical. Find this function and take a look.
Let’s take a look at the parameters of the MakeOneTag function. The first two parameters are passed by reference. The first parameter is the instance of DedeTagParse passed in, and the second parameter is the instance of
partview passed in.
Start using a loop to get the list of all tags owned by the system
$dh = dir(DEDEINC.'/taglib');
while($filename = $dh->read()){
if(ereg(".lib.",$filename)){
$alltags[] = str_replace('.lib.php','',$filename);
}
}
$dh->Close();
The code is very simple, it just traverses the /include/taglib directory, takes the file name according to the file rules, and forms an array of all tags.
The next step is a large foreach array, which traverses the CTags attribute of the instance of DedeTagParse instantiated in the partview class and parsed the current template.
We know from the previous analysis of DedeTagParse that CTags is actually a template It’s just a collection of all the tags and their parameters used in it. Here we start traversing the tags on these
templates to assign values, replace them, etc.
Let’s continue to see what this foreach does.
First get the name of the current label in the loop.
The next step is the operation on the label named field.
If it is a field tag, first get the name attribute of the tag. When the name is an array, use the Assign method of DedeTagParse to assign the partview's
The Fields array is assigned to the value attribute corresponding to the tag name (see the Assign code for details, and it is skipped here because it is relatively simple). If the label name attribute is not an array, assign the value of the specified name in the Fields array of
partview to the corresponding value of the label. The following else is very simple to make some other judgments, and then the field tags are parsed
, so we know that the field tags are equivalent to variables. Here we assign values ​​to these variables. After parsing, the loop continues Let’s analyze the next tag.
The next two ifs are for compatibility of tag names. We see that arclist has a bunch of tag names~
The next step is to see if the tags in the template have corresponding system tags. If so, , load the corresponding file, call the corresponding function, and assign the function return value to the value corresponding to the
template tag.
In this way, the tags of the entire template have been assigned values.
As for the tags in the system (those tag parsing files under /include/taglib/), it is actually a function that reads database data, organizes the data, and outputs
it.
This section ends here. It mainly talks about how the analyzed template tags are bound to the final data. In fact, it is relatively simple, because during template analysis, all tags used by the current
template have been obtained. Next, we only need to traverse the tags, call different tag functions, and obtain the function return data.
Some attentive friends will find that it is not over yet. We have obtained all the data. What next? How to generate static files?
This is what I will talk about in the next section.
15
Attachment: dedecms (v5.6) system directory
a /* Directory for generating HTML*/
|data /* Directory for saving common data generated by the program*/
| |admin /* Save the general configuration of the system background, such as author, quick navigation, source, in text format */
| |backupdata /* Database backup storage directory, which can be modified in the system background settings*/
| |cache /* System cache*/
| |enums /* Cache and js file generated by linkage category*/
| |js /* JS file generated by column js call*/
| |mark /* Picture Watermark setting directory*/
| |module /* The directory where the module installation packages are stored in the system background, usually the file names are encrypted*/
| |rss /* The directory where the generated RSSmap files are stored*/
| |sessions /* System sessions storage directory. A session will be generated after logging in. The directory must be writable*/
| |textdata /* Text data, the system background saves it as a text data storage directory*/
| |tplcache /* Template cache directory, this cache is usually those dynamic pages*/
| |ziptmp /* Compressed cache directory*/
|dede /* System background management directory*/
| |img
| |inc
| |js
| |templets /* Template storage directory in the system background*/
|images
| |js
| |swfupload
| | |images
| | |plugins
|include /* System core class library, function storage directory*/
| |calendar /* Calendar control, the calendar box that pops out of the date edit box*/
| |code /* Coding language package for datalistcp dynamic paging class*/
| |data /* System core settings data, such as the default verification code of the dictionary*/
| |dialog /* System dialog box storage directory, The selection above the editor is similar to thumbnails and inserting image files *
| | |img
| |FCKeditor /* Editor storage directory, using the open source editor FCK */
| | |editor
| | | |css
| | | | |images
| | | |dialog
| | | | |common
| | | | |fck_about
| | | |fck_codes
| | | | |fck_flash
| | | | |fck_image
| | | | |fck_link
16
| | | |dtd
| | 🎜>| | | | |smiley
| | | |js
| | | |lang
| | | |skins
| | | | |images
| |inc /* here Stores some relatively long functions introduced by users*/
| |taglib /* This is the directory where tags used in templates are stored*/
| | |channel /* ​​Extension functions that parse some special fields Library*/
| | |help /* Basic tag description*/
| |tpllib /* Dynamic template tags, generally used on dynamic call pages*/
|install /* Installation directory */
| |images
| |templates
|member /* Member Center Directory*/
| |images
| | |pay
| |inc /* Member Center* /
| |js
| |paycenter /* Payment interface configuration*/
| | |alipay
| | |cbpayment
| | |nps
| | |tenpay
>| | |yeepay
| |space /* Member center space template*/
| | |coffee
| | | |images
| | |company
| | | |images
| | |flower
| | | |images
| | |gray
| | | |images
| | |lxblog
| | | |images
| | |lxbrown
| | | |images
| | |lxvista
| | | |images
| | |person
| | | |blue
| | | |common
17
| | | | |css
| | | | |images
| | | |images
| | |pwblue
| | | |images
| | |pwglight
| | | |images
| | |pwlake
| | | |images
| | |pwpink
| | | |images
| |templets
|plus / * System plug-in storage directory*/
| |guestbook /* Message board plug-in*/
| | |images
| |img
| | |face
| |paycenter
| | |alipay
| | |cbpayment
| | |nps
| | |tenpay
| | |yeepay
| |task
|special /* Special topic storage directory*/
|templets /* Dreamweaver template storage directory*/
| |default /* System default template directory*/
| |images
| | |mood
| | |photo
| |js
| |style /* CSS style stored in the default template*/
| |plus /* Template file storage directory for the plug-in page */
| |system /* System core underlying template folder*/
| |wap /* The style directory of the wap module*/
|uploads /* File upload storage directory*/
| |allimg /* pictures*/
| |flink /* friendly link pictures*/
| |litimg /* thumbnails*/
| |media /* multimedia storage directory*/
| |soft /* software*/
| |userup /* storage of user files, such as avatars*/

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/732359.htmlTechArticlededecms code research (1) Opening dedecms I believe everyone must know this cms system, which is relatively powerful and complete. content publishing, as well as a content static system, and...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn