THINKCMF5 Update template Code Analysis, resolve template configuration JSON error causes database saved configuration item content loss problem

Source: Internet
Author: User
Tags vars

Private Function Updatethemefiles ($theme, $suffix = ' html '{$dir = ' themes/'. $theme; $themeDir = $dir; $tplFiles =[]; $root _dir_tpl_files = Cmf_scan_dir ("$dir/*. $suffix"); By default, returns an array of all the HTML file names under the template directory (w0s directory) foreach ($root _dir_tpl_files as $root _tpl_file{$root _tpl_file = "$dir/$root _tpl_file"; $configFile = Preg_replace ("/\. $suffix $/", '. Json ', $root _tpl_file); Gets the current $root_tpl_file corresponding to a JSON file name such as Index.json $root _tpl_file_no_suffix = preg_replace ("/\. $suffix $/", ", $root _tpl_ File); Get the corresponding file name no suffix if (is_file ($root _tpl_file) && file_exists_case ($configFile) {//current $root_tpl_file (for example: index.html) is a file, and Index.json exists Array_push ($tplFiles, $root _tpl_file_no_suffix); Save the file name with no suffix. For example: Index}} $subDirs = Cmf_sub_dirs ($dir); Sub-directory files, is not only support level two directory???? foreach ($subDirs as $dir{$subDirTplFiles = Cmf_scan_dir ("$dir/*. $suffix"); foreach ($subDirTplFiles as $tplFile{$tplFile = "$dir/$tplFile"; $configFile = Preg_replace ("/\. $suffix $/", '. Json ', $tplFile); $tplFileNoSuffix = Preg_replace ("/\. $suffix $/", ", $tplFile); if (Is_file ($tplFile) && file_exists_case ($configFile) {Array_push ($tplFiles, $tplFileNoSuffix); }}} foreach ($tplFiles as $tplFile{//Traverse all file json files $configFile = $tplFile. ". JSON"; $file = preg_replace ('/^themes\/'. $theme. ' \//', ', $tplFile); $file = Strtolower ($file); $config = Json_decode (file_get_contents ($configFile), True); $findFile = Db::name (' theme_file ')->where ([' theme ' + = $theme, ' file ' + $file])Find (); $isPublic = Empty ($config [' is_public '])? 0:1; $listOrder = Empty ($config [' Order '])? 0:floatval ($config [' Order ']); $configMore = Empty ($config [' more '])? []: $config [' More ']; $more = $configMore; if (Empty ($findFile) {//Theme_file the template is not found in datasheet insert Db::name (' Theme_file ')Insert ([' theme ' = + $theme, ' action ' + $config [' action '], ' file ' + $file, ' name ' = ' + ' $config [' name '], ' more ' = = Json_encode ($more), ' config_more ' = Json_encode ($configMore), ' description ' + $config [' description '], ' Is_public ' = $isPublic, ' List_order ' and $listOrder]); } else {//update file $moreInDb = Json_decode ($findFile [' More '], true); The more field read from the database $more = $this->updatethemeconfigmore ($configMore, $moreInDb); $configMore the JSON read from the Index.json file, $moreInDb the value of the more field read from the database Db::name (' Theme_file ')->where (' Theme ' = $theme , ' file ' = $file])update ([' theme ' + $theme, ' action ' + $config [' action '], ' file ' + $file, ' name ' = = $config [' name '], ' mo Re ' = Json_encode ($more),//json file has and value from the data, if $configmore is not read correctly, it should be returned a null value, not saved successfully ' Config_more ' + JSON _encode ($configMore),//config file from Index.json ' description ' + $config [' description '], ' is_public ' + $isPublic, ' List_order ' = $listOrder ]); }//Check if the installed template file has been deleted $files = Db::name (' theme_file ')->where ([' theme ' = ' = $theme]), select (); foreach ($ Files as $themeFile) {$tplFile = $themeDir. ‘/‘ . $themeFile [' file ']. ‘.‘ . $suffix; $tplFileConfigFile = $themeDir. ‘/‘ . $themeFile [' file ']. '. JSON '; if (!is_file ($tplFile) | | |!file_exists_case ($tplFileConfigFile)) {db::name (' theme_file ')->where ([' Theme ' = $theme, ' file ' = ' = $themeFile [' file ']]),Delete ();}} }

/** * Alternate Scan_dir method * @param string $pattern search mode *.txt,*.doc; (same Glog method) * @param int $flags * @param $pattern * @return array */function cmf_scan_dir ($pattern, $flags = null) {    $files = Glob ($pattern, $flags);   The function returns the file name or directory that matches the specified pattern. The function returns an array that contains a matching file/directory. Returns False if an error occurs. Http://www.w3school.com.cn/php/func_filesystem_glob.asp    if (empty ($files)) {        $files = [];    } else {        $files = array_map (' basename ', $files);//The function functions the user-defined function to each value in the array and returns an array with the new value after the user-defined function. The basename function returns the file name portion of the path,} return $files;//An array of file names for the specified rule, and when the template is updated, returns all. html filename with extension http://www.w3school.com.cn/ Php/func_filesystem_basename.asp. }

Private Function Updatethemeconfigmore ($configMore, $moreInDb)    {

echo Empty ($configMore);
Print_r ($configMore);
Return

if (!empty ($configMore [' VARs ']) {foreach ($configMore [' VARs '] as $mVarName + = $mVar{if (Isset ($moreInDb [' VARs '] [$mVarName] [' value ']) && $mVar [' type '] = = $moreInDb [' VARs '] [$mVarNam e][' type '] {$configMore [' vars '] [$mVarName] [' value '] = $moreInDb [' VARs '] [$mVarName] [' value ']];                    The database has this vars, and the same type, it updates the value into the configuration file array. if (Isset ($moreInDb [' VARs '] [$mVarName] [' ValueText ']) {$configMore [' vars '] [$mVarName] [' valueText '] = $moreInDb [' VARs '] [$mVarName] [' ValueText ']; }}} if (!empty ($configMore [' Widgets ']) {foreach ($configMore [' Widgets '] as $widgetName + = $widget{if (Isset ($moreInDb [' Widgets '] [$widgetName] [' title ']) {$configMore [' widgets '] [$widgetName] [' title '] = $moreInDb [' Widgets '] [$widgetName] [' title ']; if (Isset ($moreInDb [' Widgets '] [$widgetName] [' Display ']) {$configMore [' widgets '] [$widgetName] [' display '] = $ moreindb[' Widgets ' [$widgetName] [' Display '];} if (!empty ($widget [' VARs '])}{foreach ($widget [' VARs '] as $ Widgetvarname = $widgetVar) {if (Isset ($moreInDb [' Widgets '] [$widgetName] [' VARs '] [$widgetVarName] [' value ']) && $widgetVar [' type '] = = $moreInDb [' Widgets '] [$widgetName] [' VARs '] [$widgetVarName] [' type ']} {$ configmore[' Widgets ' [$widgetName] [' VARs '] [$widgetVarName] [' value '] = $moreInDb [' Widgets '] [$widgetName] [' VARs '] [ $widgetVarName] [' value ']; if (Isset ($moreInDb [' Widgets '] [$widgetName] [' VARs '] [$widgetVarName] [' ValueText ' ]) {$configMore [' widgets '] [$widgetName] [' VARs '] [$widgetVarName] [' valueText '] = $moreInDb [' Widgets '][$ widgetname][' VARs ' [$widgetVarName] [' ValueText '];} }}}} return $configMore;}         

The problem is key in function Updatethemeconfigmore ($configMore, $moreInDb); If the JSON file has a syntax error, the $configMore is not read correctly. May cause configuration loss.

At the beginning of the function Updatethemeconfigmore, add the following code to output the JSON-generated array

echo Empty ($configMore). " \ n ";
Print_r ($configMore);
Return

Template update, when a JSON file has a syntax error (such as a comma), the value of $configMore is an empty array, and the empty ($configMore) value returns 1.

themes/w0s/portal/index1

Array()

Solution The Updatethemefiles function adds a judgment on the JSON conversion result, and the conversion succeeds in updating the database.

Private Function Updatethemefiles ($theme, $suffix = ' html '{$dir = ' themes/'. $theme; $themeDir = $dir; $tplFiles =[]; $root _dir_tpl_files = Cmf_scan_dir ("$dir/*. $suffix"); foreach ($root _dir_tpl_files as $root _tpl_file{$root _tpl_file = "$dir/$root _tpl_file"; $configFile = Preg_replace ("/\. $suffix $/", '. Json ', $root _tpl_file); $root _tpl_file_no_suffix = Preg_replace ("/\. $suffix $/", ", $root _tpl_file); if (Is_file ($root _tpl_file) && file_exists_case ($configFile) {Array_push ($tplFiles, $root _tpl_file_no_suffix); }} $subDirs = Cmf_sub_dirs ($dir); foreach ($subDirs as $dir{$subDirTplFiles = Cmf_scan_dir ("$dir/*. $suffix"); foreach ($subDirTplFiles as $tplFile{$tplFile = "$dir/$tplFile"; $configFile = Preg_replace ("/\. $suffix $/", '. Json ', $tplFile); $tplFileNoSuffix = Preg_replace ("/\. $suffix $/", ", $tplFile); if (Is_file ($tplFile) && file_exists_case ($configFile) {Array_push ($tplFiles, $tplFileNoSuffix); }}} foreach ($tplFiles as $tplFile{$configFile = $tplFile. ". JSON"; $file = preg_replace ('/^themes\/'. $theme. ' \//', ', $tplFile); $file = Strtolower ($file); $config = Json_decode (file_get_contents ($configFile), True); $findFile = Db::name (' theme_file ')->where ([' theme ' + = $theme, ' file ' + $file])Find (); $isPublic = Empty ($config [' is_public '])? 0:1; $listOrder = Empty ($config [' Order '])? 0:floatval ($config [' Order ']); $configMore = Empty ($config [' more '])? []: $config [' More ']; $more = $configMore;//json does not update the database if it is not successfully converted (!empty ($config)) {if (empty ($findFile)) {db::name (' theme_file ')->insert ([' theme ' = ' = ' $theme, ' action ' + $config [' action '], ' File ' = ' $file, ' name ' + $config [' name '], ' more ' = Json_encode ($more), ' config_more ' = Json_encode ($config more), ' description ' = $config [' description '], ' is_public ' and ' = $isPublic, ' list_order ' + $listOrder]); } else {//update file $moreInDb = Json_decode ($findFile [' More '], true);//echo "\ n". $tplFile. " \ n "; $more = $this->updatethemeconfigmore ($configMore, $moreInDb); echo Empty ($more). " \ n "; Db::name (' Theme_file ')->where ([' theme ' = = $theme, ' file ' = ' $file])->update ([' theme ' + = $theme, ' action ' = = $config [' Action '], ' file ' and ' = ' $file, ' name ' and ' + ' $config [' name '], ' more ' = Json_encode ($more), ' Config_more ' = = Json_encode ($configMore), ' description ' = ' $config [' description '], ' is_public ' + $isPublic, ' List_order ' = $listOrder ]); } } }

Thinkcmf5 Update template code Analysis, resolve template configuration JSON error causes database saved configuration item content loss problem

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.