From 8f7cd55464555b88133f208e9e61aa4d3b64eb76 Mon Sep 17 00:00:00 2001 From: Cameron Date: Mon, 18 Sep 2017 13:43:31 -0700 Subject: [PATCH] Fixes #2786 Plugin Builder > Check language files --- e107_admin/plugin.php | 928 ++++++++++---------- e107_admin/users.php | 2 +- e107_languages/English/admin/lan_plugin.php | 5 +- 3 files changed, 477 insertions(+), 458 deletions(-) diff --git a/e107_admin/plugin.php b/e107_admin/plugin.php index 911503aa6..e69d356b8 100644 --- a/e107_admin/plugin.php +++ b/e107_admin/plugin.php @@ -82,6 +82,12 @@ class plugman_adminArea extends e_admin_dispatcher 'ui' => 'plugin_form_ui', 'uipath' => null ), + 'lans' => array( + 'controller' => 'pluginLanguage', + 'path' => null, + 'ui' => 'plugin_form_ui', + 'uipath' => null + ), ); @@ -104,8 +110,8 @@ class plugman_adminArea extends e_admin_dispatcher protected $adminMenuAliases = array( - 'installed/uninstall' => 'installed/list' - + 'installed/uninstall' => 'installed/list', + 'lans/list' => 'create/build' ); protected $adminMenuIcon = 'e-plugmanager-24'; @@ -376,8 +382,13 @@ class plugin_ui extends e_admin_ui } + function lanPage() + { + + } + function uninstallPage() { $id = $this->getId(); @@ -1524,6 +1535,457 @@ class plugin_form_online_ui extends e_admin_form_ui +class pluginLanguage extends e_admin_ui +{ + + private $scriptFiles = array(); + private $lanFiles = array(); + + private $lanDefs = array(); + private $scriptDefs = array(); + + private $lanDefsData = array(); + private $scriptDefsData = array(); + + private $unused = array(); + private $unsure = array(); + + private $excludeLans = array('CORE_LC', 'CORE_LC2', 'e_LAN', 'e_LANGUAGE', 'e_LANGUAGEDIR', 'LAN', 'LANGUAGE'); + + private $useSimilar = false; + + + function listPage() + { + if(!empty($_GET['newplugin']) && $_GET['step']==2) + { + $plugin = e107::getParser()->filter($_GET['newplugin'],'file'); + return $this->step2($plugin); + + } + + } + + + + + + function step2($path) + { + $this->plugin = $path; + + $fl = e107::getFile(); + + $files = $fl->get_files(e_PLUGIN.$path.'/languages',null,null,3); + $files2 = $fl->get_files(e_PLUGIN.$path,'\.php|\.sc|\.bb|\.xml','languages',3); + + $this->scanLanFile(e_LANGUAGEDIR."English/English.php"); + $this->scanLanFile(e_LANGUAGEDIR."English/admin/lan_admin.php"); + + foreach($files as $v) + { + if(strpos($v['path'],'English')!==false OR strpos($v['fname'],'English')!==false) + { + $path = $v['path'].$v['fname']; + $this->lanFiles[] = $path; + + $this->scanLanFile($path); + } + } + + foreach($files2 as $v) + { + $path = $v['path'].$v['fname']; + $this->scriptFiles[] = $path; + $this->scanScriptFile($path); + } + + + return $this->renderResults(); + + } + + + function findSimilar($data) + { + $sim = array(); + + foreach($this->lanDefsData as $k=>$v) + { + if(empty($v['value'])) + { + continue; + } + + if($this->useSimilar == true) + { + similar_text($v['value'], $data['value'], $percentSimilar); + } + else + { + $percentSimilar = 0; + } + + if((($v['value'] == $data['value'] || $percentSimilar > 89) && $data['file'] != $v['file'])) + { + if(strpos($v['lan'],'LAN')===false) // Defined constants that don't contain 'LAN'. + { + $v['status'] = 2; + } + else + { + $v['status'] = (in_array($v['lan'],$this->used)) ? 1 : 0; + } + + $sim[] = $v; + + } + } + + + + return $sim; + + } + + + function renderSimilar($data,$mode='') + { + + $sim = $this->findSimilar($data); + + + if(empty($sim) || ($mode == 'script' && count($sim) < 2)) + { + return; // ADMIN_TRUE_ICON; + } + + $text = " + "; + + foreach($sim as $k=>$val) + { + $text .= " + + + + "; + + } + + $text .= "
".$this->shortPath($val['file'])."".$val['lan']."
".$val['value']."
".$this->renderStatus($val['status'])."
"; + return $text; + + } + + function renderFilesList($list) + { + $l= array(); + foreach($list as $v) + { + $l[] = $this->shortPath($v,'script'); + + + } + + if(!empty($l)) + { + return implode("
",$l); + } + + + } + + function renderStatus($val,$mode='lan') + { + $diz = array( + 'lan' => array(0 => 'Unused by '.$this->plugin, 1=>'Used by '.$this->plugin, 2=>'Unsure'), + 'script' => array(0=> 'Missing from Language Files', 1=>'Found in Language Files', 3=>"Generic") + ); + + + + if($val ==1) + { + return "".$diz[$mode][$val].""; + } + + if($val == 2) + { + return "".$diz[$mode][$val].""; + } + + return "".$diz[$mode][$val].""; + } + + function shortPath($path,$mode='lan') + { + + if($path == e_LANGUAGEDIR.'English/English.php') + { + return "Core Frontend Language File"; + } + + if($path == e_LANGUAGEDIR.'English/admin/lan_admin.php') + { + return "Core Admin-area Language File"; + } + + if($mode == 'script') + { + return str_replace(e_PLUGIN.$this->plugin.'/','',$path); + } + else + { + + $text = str_replace(e_PLUGIN.$this->plugin.'/languages/','',$path); + + if(strpos($path,'_front.php')===false && strpos($path,'_admin.php')===false && strpos($path,'_global.php')===false && strpos($path,'_menu.php')===false && strpos($path,'_notify.php')===false && strpos($path,'_search.php')===false) + { + return "".$text.""; + } + + return $text; + + } + + } + + + function renderTable($array,$mode) + { + if(empty($array)) + { + return "
No Matches
"; + } + + $text2 = ''; + + if($mode == 'unsure') + { + $text2 .= "
LAN items in this list have been named incorrectly. They should include 'LAN' in their name. eg. LAN_".strtoupper($this->plugin)."_001
"; + + } + + $text2 .= " + + + + + + + "; + + foreach($array as $k=>$v) + { + $text2 .= " + + + + + "; + + } + + + $text2 .= "
LANFileValueDuplicate or Similar Value
".$v."".$this->shortPath($this->lanDefsData[$k]['file'])."".$this->lanDefsData[$k]['value']."".$this->renderSimilar($this->lanDefsData[$k])."
"; + + return $text2; + } + + function renderScriptTable() + { + + // return print_a($this->scriptDefsData,true); + + $text2 = " + + + + + + + + + + "; + + foreach($this->scriptDefsData as $k=>$v) + { + $status = in_array($v['lan'],$this->lanDefs) ? 1 : 0; + // $lan = $v['lan']; + // $v['value'] = $this->lanDefsRaw[$lan]; + // $sim = $this->findSimilar($v); + + $text2 .= " + + + + + + + + "; + + } + + + $text2 .= "
idFileDetected LANLAN ValueFound on LineStatusDuplicates / Possible Substitions
".$k."".$this->shortPath($v['file'],'script')."".$v['lan']."".$this->lanDefsRaw[$v['lan']]."".$v['line']."".$this->renderStatus($status,'script')."".$this->renderSimilar($v,'script')."
"; + + return $text2; + + } + + + function renderResults() + { + $frm = e107::getForm(); + $ns = e107::getRender(); + + $this->unused = array_diff($this->lanDefs,$this->scriptDefs); + + $this->used = array_intersect($this->lanDefs,$this->scriptDefs); + + foreach($this->unused as $k=>$v) + { + if(strpos($v,'LAN')===false) + { + unset($this->unused[$k]); + $this->unsure[$k] = $v; + } + + if(strpos($this->lanDefsData[$k]['file'],$this->plugin) === false || in_array($v,$this->excludeLans)) + { + unset($this->unused[$k]); + unset($this->unsure[$k]); + } + + + } + +// print_a($this->scriptData); + + $used = $this->renderTable($this->used, 'used'); + $unused = $this->renderTable($this->unused,'unused'); + $unsure = $this->renderTable($this->unsure,'unsure'); + + + // echo $text2; + $tabs = array ( + 0 => array('caption'=>EPL_ADLAN_222, 'text'=> $this->renderScriptTable()), + 1 => array('caption'=>EPL_ADLAN_223, 'text'=>$used), + 2 => array('caption'=>EPL_ADLAN_224, 'text'=>$unused), + 3 => array('caption'=>EPL_ADLAN_225, 'text'=>$unsure), + + + ); + + + $this->addTitle(ADLAN_98.SEP.EPL_ADLAN_114.SEP.EPL_ADLAN_221.SEP.$this->plugin); + + $text = "
".LAN_BACK."
"; + + return $frm->tabs($tabs).$text; + + //$ns->tablerender(ADLAN_98.SEP.EPL_ADLAN_114.SEP.EPL_ADLAN_221.SEP.$this->plugin, $frm->tabs($tabs)); + + } + + + + + + + function scanScriptFile($path) + { + $lines = file($path, FILE_IGNORE_NEW_LINES); + + foreach($lines as $ln=>$row) + { + $row = trim($row); + if(substr($row,0,2) == '/*') + { + // $skip =true; ; + + } + if(substr($row,0,2) == '*/') + { + // $skip =false; + // continue; + } + + if(empty($row) || $skip == true || substr($row,0,5) == '' || substr($row,0,2)=='//') + { + continue; + } + + if(preg_match_all("/([\w_]*LAN[\w_]*)/", $row, $match)) + { + foreach($match[1] as $lan) + { + if(!in_array($lan,$this->excludeLans)) + { + $this->scriptDefs[] = $lan; + $this->scriptDefsData[] = array('file'=>$path, 'line'=>$ln, 'lan'=>$lan, 'value'=>$this->lanDefsRaw[$lan]); + // $this->scriptData[$path][$ln] = $row; + } + } + } + } + + + } + + + function scanLanFile($path) + { + + + $data = file_get_contents($path); + + if(preg_match_all('/(\/\*[\s\S]*?\*\/)/i',$data, $multiComment)) + { + $data = str_replace($multiComment[1],'',$data); // strip multi-line comments. + } + + + $type = basename($path); + + if(preg_match_all('/^\s*?define\s*?\(\s*?(\'|\")([\w]+)(\'|\")\s*?,\s*?(\'|\")([\s\S]*?)\s*?(\'|\")\s*?\)\s*?;/im',$data,$matches)) + { + $def = $matches[2]; + $values = $matches[5]; + + foreach($def as $k=>$d) + { + if($d == 'e_PAGETITLE' || $d == 'PAGE_NAME' || $d =='CORE_LC' && $d =='CORE_LC2') + { + continue; + } + + $retloc[$type][$d]= $values[$k]; + $this->lanDefs[] = $d; + $this->lanDefsData[] = array('file'=>$path, 'lan'=>$d, 'value'=>$values[$k]); + $this->lanDefsRaw[$d] = $values[$k]; + } + } + + //print_a($this->lanDefsData); + return; + } + + + + + + +} + + + + + + @@ -2965,457 +3427,6 @@ function plugin_adminmenu() -class pluginLanguage -{ - - private $scriptFiles = array(); - private $lanFiles = array(); - - private $lanDefs = array(); - private $scriptDefs = array(); - - private $lanDefsData = array(); - private $scriptDefsData = array(); - - private $unused = array(); - private $unsure = array(); - - private $excludeLans = array('CORE_LC', 'CORE_LC2', 'e_LAN', 'e_LANGUAGE', 'e_LANGUAGEDIR', 'LAN', 'LANGUAGE'); - - private $useSimilar = false; - - - function __construct() - { - - if(!empty($_GET['newplugin']) && $_GET['step']==2) - { - $plugin = e107::getParser()->filter($_GET['newplugin'],'file'); - $this->step2($plugin); - return false; - } - - - - // return $this->step1(); - } - - - - - - function step2($path) - { - $this->plugin = $path; - - $fl = e107::getFile(); - - $files = $fl->get_files(e_PLUGIN.$path.'/languages',null,null,3); - $files2 = $fl->get_files(e_PLUGIN.$path,'\.php|\.sc|\.bb|\.xml','languages',3); - - $this->scanLanFile(e_LANGUAGEDIR."English/English.php"); - $this->scanLanFile(e_LANGUAGEDIR."English/admin/lan_admin.php"); - - foreach($files as $v) - { - if(strpos($v['path'],'English')!==false OR strpos($v['fname'],'English')!==false) - { - $path = $v['path'].$v['fname']; - $this->lanFiles[] = $path; - - $this->scanLanFile($path); - } - } - - foreach($files2 as $v) - { - $path = $v['path'].$v['fname']; - $this->scriptFiles[] = $path; - $this->scanScriptFile($path); - } - - - $this->renderResults(); - - return true; - } - - - function findSimilar($data) - { - $sim = array(); - - foreach($this->lanDefsData as $k=>$v) - { - if(empty($v['value'])) - { - continue; - } - - if($this->useSimilar == true) - { - similar_text($v['value'], $data['value'], $percentSimilar); - } - else - { - $percentSimilar = 0; - } - - if((($v['value'] == $data['value'] || $percentSimilar > 89) && $data['file'] != $v['file'])) - { - if(strpos($v['lan'],'LAN')===false) // Defined constants that don't contain 'LAN'. - { - $v['status'] = 2; - } - else - { - $v['status'] = (in_array($v['lan'],$this->used)) ? 1 : 0; - } - - $sim[] = $v; - - } - } - - - - return $sim; - - } - - - function renderSimilar($data,$mode='') - { - - $sim = $this->findSimilar($data); - - - if(empty($sim) || ($mode == 'script' && count($sim) < 2)) - { - return; // ADMIN_TRUE_ICON; - } - - $text = " - "; - - foreach($sim as $k=>$val) - { - $text .= " - - - - "; - - } - - $text .= "
".$this->shortPath($val['file'])."".$val['lan']."
".$val['value']."
".$this->renderStatus($val['status'])."
"; - return $text; - - } - - function renderFilesList($list) - { - $l= array(); - foreach($list as $v) - { - $l[] = $this->shortPath($v,'script'); - - - } - - if(!empty($l)) - { - return implode("
",$l); - } - - - } - - function renderStatus($val,$mode='lan') - { - $diz = array( - 'lan' => array(0 => 'Unused by '.$this->plugin, 1=>'Used by '.$this->plugin, 2=>'Unsure'), - 'script' => array(0=> 'Missing from Language Files', 1=>'Found in Language Files', 3=>"Generic") - ); - - - - if($val ==1) - { - return "".$diz[$mode][$val].""; - } - - if($val == 2) - { - return "".$diz[$mode][$val].""; - } - - return "".$diz[$mode][$val].""; - } - - function shortPath($path,$mode='lan') - { - - if($path == e_LANGUAGEDIR.'English/English.php') - { - return "Core Frontend Language File"; - } - - if($path == e_LANGUAGEDIR.'English/admin/lan_admin.php') - { - return "Core Admin-area Language File"; - } - - if($mode == 'script') - { - return str_replace(e_PLUGIN.$this->plugin.'/','',$path); - } - else - { - - $text = str_replace(e_PLUGIN.$this->plugin.'/languages/','',$path); - - if(strpos($path,'_front.php')===false && strpos($path,'_admin.php')===false && strpos($path,'_global.php')===false && strpos($path,'_menu.php')===false && strpos($path,'_notify.php')===false && strpos($path,'_search.php')===false) - { - return "".$text.""; - } - - return $text; - - } - - } - - - function renderTable($array,$mode) - { - if(empty($array)) - { - return "
No Matches
"; - } - - $text2 = ''; - - if($mode == 'unsure') - { - $text2 .= "
LAN items in this list have been named incorrectly. They should include 'LAN' in their name. eg. LAN_".strtoupper($this->plugin)."_001
"; - - } - - $text2 .= " - - - - - - - "; - - foreach($array as $k=>$v) - { - $text2 .= " - - - - - "; - - } - - - $text2 .= "
LANFileValueDuplicate or Similar Value
".$v."".$this->shortPath($this->lanDefsData[$k]['file'])."".$this->lanDefsData[$k]['value']."".$this->renderSimilar($this->lanDefsData[$k])."
"; - - return $text2; - } - - function renderScriptTable() - { - - // return print_a($this->scriptDefsData,true); - - $text2 = " - - - - - - - - - - "; - - foreach($this->scriptDefsData as $k=>$v) - { - $status = in_array($v['lan'],$this->lanDefs) ? 1 : 0; - // $lan = $v['lan']; - // $v['value'] = $this->lanDefsRaw[$lan]; - // $sim = $this->findSimilar($v); - - $text2 .= " - - - - - - - - "; - - } - - - $text2 .= "
idFileDetected LANLAN ValueFound on LineStatusDuplicates / Possible Substitions
".$k."".$this->shortPath($v['file'],'script')."".$v['lan']."".$this->lanDefsRaw[$v['lan']]."".$v['line']."".$this->renderStatus($status,'script')."".$this->renderSimilar($v,'script')."
"; - - return $text2; - - } - - - function renderResults() - { - $frm = e107::getForm(); - $ns = e107::getRender(); - - $this->unused = array_diff($this->lanDefs,$this->scriptDefs); - - $this->used = array_intersect($this->lanDefs,$this->scriptDefs); - - foreach($this->unused as $k=>$v) - { - if(strpos($v,'LAN')===false) - { - unset($this->unused[$k]); - $this->unsure[$k] = $v; - } - - if(strpos($this->lanDefsData[$k]['file'],$this->plugin) === false || in_array($v,$this->excludeLans)) - { - unset($this->unused[$k]); - unset($this->unsure[$k]); - } - - - } - -// print_a($this->scriptData); - - $used = $this->renderTable($this->used, 'used'); - $unused = $this->renderTable($this->unused,'unused'); - $unsure = $this->renderTable($this->unsure,'unsure'); - - - // echo $text2; - $tabs = array ( - 0 => array('caption'=>EPL_ADLAN_222, 'text'=> $this->renderScriptTable()), - 1 => array('caption'=>EPL_ADLAN_223, 'text'=>$used), - 2 => array('caption'=>EPL_ADLAN_224, 'text'=>$unused), - 3 => array('caption'=>EPL_ADLAN_225, 'text'=>$unsure), - - - ); - - - - $ns->tablerender(ADLAN_98.SEP.EPL_ADLAN_114.SEP.EPL_ADLAN_221.SEP.$this->plugin, $frm->tabs($tabs)); - - } - - - - - - - function scanScriptFile($path) - { - $lines = file($path, FILE_IGNORE_NEW_LINES); - - foreach($lines as $ln=>$row) - { - $row = trim($row); - if(substr($row,0,2) == '/*') - { - // $skip =true; ; - - } - if(substr($row,0,2) == '*/') - { - // $skip =false; - // continue; - } - - if(empty($row) || $skip == true || substr($row,0,5) == '' || substr($row,0,2)=='//') - { - continue; - } - - if(preg_match_all("/([\w_]*LAN[\w_]*)/", $row, $match)) - { - foreach($match[1] as $lan) - { - if(!in_array($lan,$this->excludeLans)) - { - $this->scriptDefs[] = $lan; - $this->scriptDefsData[] = array('file'=>$path, 'line'=>$ln, 'lan'=>$lan, 'value'=>$this->lanDefsRaw[$lan]); - // $this->scriptData[$path][$ln] = $row; - } - } - } - } - - - } - - - function scanLanFile($path) - { - - - $data = file_get_contents($path); - - if(preg_match_all('/(\/\*[\s\S]*?\*\/)/i',$data, $multiComment)) - { - $data = str_replace($multiComment[1],'',$data); // strip multi-line comments. - } - - - $type = basename($path); - - if(preg_match_all('/^\s*?define\s*?\(\s*?(\'|\")([\w]+)(\'|\")\s*?,\s*?(\'|\")([\s\S]*?)\s*?(\'|\")\s*?\)\s*?;/im',$data,$matches)) - { - $def = $matches[2]; - $values = $matches[5]; - - foreach($def as $k=>$d) - { - if($d == 'e_PAGETITLE' || $d == 'PAGE_NAME' || $d =='CORE_LC' && $d =='CORE_LC2') - { - continue; - } - - $retloc[$type][$d]= $values[$k]; - $this->lanDefs[] = $d; - $this->lanDefsData[] = array('file'=>$path, 'lan'=>$d, 'value'=>$values[$k]); - $this->lanDefsRaw[$d] = $values[$k]; - } - } - - //print_a($this->lanDefsData); - return; - } - - - - - - -} - - - - - - /** * Plugin Admin Generator by CaMer0n. //TODO - Added dummy template and shortcode creation, plus e_search, e_cron, e_xxxxx etc. @@ -3508,6 +3519,7 @@ class pluginBuilder $newDir[$dir] = $dir; } + $info = EPL_ADLAN_102; $info .= ""; - $mes->addInfo($tp->toHtml($info,true)); + // $mes->addInfo($tp->toHtml($info,true)); $text = $frm->open('createPlugin','get', e_SELF); $text .= $frm->hidden('action', 'build'); + $text .= "+ - + + - + + "; diff --git a/e107_admin/users.php b/e107_admin/users.php index 25e7f5783..d94811959 100644 --- a/e107_admin/users.php +++ b/e107_admin/users.php @@ -1209,7 +1209,7 @@ class users_admin_ui extends e_admin_ui } else { - $mes->addError($email.' - '.USRLAN_234, 'default', true); + $mes->addError($email.' - '.USRLAN_234, 'default', true); // Invalid. $this->redirect('list', 'main', true); } diff --git a/e107_languages/English/admin/lan_plugin.php b/e107_languages/English/admin/lan_plugin.php index 5d482aa00..50483d437 100644 --- a/e107_languages/English/admin/lan_plugin.php +++ b/e107_languages/English/admin/lan_plugin.php @@ -308,5 +308,8 @@ define("LAN_INSTALL_FAIL", "Installation failed!"); define("LAN_UNINSTALL_FAIL", "Unable to uninstall!"); define("LAN_PLUGIN_IS_USED", "[x] plugin is used by:"); +define("EPL_ADLAN_254", "This will check your plugin's language files for errors and common or duplicate LAN definitions. "); -?> + + +?> \ No newline at end of file
".EPL_ADLAN_107."
".$frm->open('createPlugin','get',e_SELF."?mode=create").$frm->select("newplugin",$newDir).$frm->admin_button('step', 2,'other',LAN_GO)."
".$frm->checkbox('createFiles',1,1,EPL_ADLAN_232).$frm->close()."
".$frm->open('createPlugin','get',e_SELF."?mode=create").$frm->select("newplugin",$newDir, false, 'size=xlarge').$frm->admin_button('step', 2,'other',LAN_GO)."
".$frm->checkbox('createFiles',1,1,EPL_ADLAN_232).$frm->close()."
".$info."
".EPL_ADLAN_108."
".$frm->open('checkPluginLangs','get',e_SELF."?mode=lans").$frm->select("newplugin",$lanDir).$frm->admin_button('step', 2,'other',LAN_GO)."
".$frm->close()."
".$frm->open('checkPluginLangs','get',e_SELF."?mode=lans").$frm->select("newplugin",$lanDir, false, 'size=xlarge').$frm->admin_button('step', 2,'other',LAN_GO)."
".$frm->close()."
".EPL_ADLAN_254."