Skip to content

Commit

Permalink
add updater script
Browse files Browse the repository at this point in the history
  • Loading branch information
davydovct committed Sep 21, 2018
1 parent a494767 commit 5168069
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 6 deletions.
6 changes: 3 additions & 3 deletions cleantalk.antispam/description.en
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
- "Move JS code to external file"
- "Update CCF option"
- "Update custom config"
- "Bitrix-composite support"
- "Update SFW logic"
- "Bug fixes and other minor improvements"
6 changes: 3 additions & 3 deletions cleantalk.antispam/description.ru
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
- "Весь подключаемый JS код вынесен в отдельный файл"
- "Обновлена опция кастомных форм"
- "Обновлен кастомный конфиг по исключению страниц из фильтрации"
- "Поддержка композитного режима"
- "Обновление логики SFW"
- "Устранение ошибок и другие улучшения"
1 change: 1 addition & 0 deletions cleantalk.antispam/install/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ function InstallFiles() {
$this->ct_template_addon_tag,
$this->ct_template_addon_body
);

if($SAR_res != 0){
$this->errors[] = GetMessage('CLEANTALK_ERROR_FILES_'.sprintf('%02d', $SAR_res));
$ret_val = FALSE;
Expand Down
84 changes: 84 additions & 0 deletions cleantalk.antispam/updater.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?php
if(IsModuleInstalled('cleantalk.antispam'))
{
// Values for all templates
$ct_template_addon_tag = 'CLEANTALK template addon';
$ct_template_addon_body = "\n" . '<?php \Bitrix\Main\Page\Frame::getInstance()->startDynamicWithID("area"); if(CModule::IncludeModule("cleantalk.antispam")) echo CleantalkAntispam::FormAddon(); \Bitrix\Main\Page\Frame::getInstance()->finishDynamicWithID("area", "Loading..."); ?>' . "\n";

// Values for templates folder
$template_file = 'footer.php';
//...with ending slash
$local_template_dir = $DOCUMENT_ROOT.'/bitrix/templates/';
$pattern = '/(<\/body>)/i';
// Check system folders
if(!file_exists($local_template_dir)){
// No required system folders
return;
}
$all_templates_folder = glob($local_template_dir . '/*' , GLOB_ONLYDIR);

if (file_exists($local_template_dir .'/.default'))
$all_templates_folder[] = $local_template_dir .'/.default';

foreach ($all_templates_folder as $current_template)
{
$template_file_path = $current_template.'/'.$template_file;
// Last check - template PHP file
if(!file_exists($template_file_path) || !is_file($template_file_path) || !is_writable($template_file_path)){
// No template PHP file
return;
}

// Here we are sure that
// bitrix/templates/<template>/components/bitrix/<component>/<template>/<file>.php
// exists and writable

// Try to get template PHP file content
$template_content = file_get_contents($template_file_path);
if($template_content === FALSE){
// Cannot read from template PHP file
return;
}

// Check is it parsable
if(!preg_match($pattern, $template_content) === 1){
// Cannot find pattern for addon inserting in template PHP file
return;
}
// First clean all previous CLEANTALK template addons
$ct_template_addon_begin = '<!-- ' . $ct_template_addon_tag . ' -->'; // don't change this!
$ct_template_addon_end = '<!-- /' . $ct_template_addon_tag . ' -->'; // don't change this!

$pos_begin = strpos($template_content, $ct_template_addon_begin);
$pos_end = strpos($template_content, $ct_template_addon_end);

if($pos_begin !== FALSE && $pos_end === FALSE){
// Cannot parse template PHP file - old CLEANTALK open tag exists only
return;
}elseif($pos_begin === FALSE && $pos_end !== FALSE){
// Cannot parse template PHP file - old CLEANTALK close tag exists only
return;
}elseif($pos_begin !== FALSE && $pos_end !== FALSE){
if($pos_begin < $pos_end){
// Cleaning needed
$template_content = substr($template_content, 0, $pos_begin) . substr($template_content, $pos_end + strlen($ct_template_addon_end));
}else{
// Cannot parse template PHP file - old CLEANTALK close tag before open tag
return;
}
//}elseif($pos_begin === FALSE && $pos_end === FALSE){
// // Nothing to clean
}
// Second add current CLEANTALK template addon

$ct_template_addon = $ct_template_addon_begin . $ct_template_addon_body . $ct_template_addon_end . "\n\n";

$template_content = preg_replace($pattern, $ct_template_addon . '${1}', $template_content, 1);

if(!file_put_contents($template_file_path, $template_content)){
// Cannot write new content to template PHP file
return;
}

}
}

0 comments on commit 5168069

Please sign in to comment.