Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Aug 6th, 2012  |  syntax: None  |  size: 8.97 KB  |  hits: 15  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. <?php
  2. /**
  3. *
  4. * @package Language File Conflict Detector
  5. * @version $Id$
  6. * @copyright (c) 2012 nickvergessen nickvergessen@gmx.de http://www.flying-bits.org
  7. * @license http://opensource.org/licenses/gpl-license.php GNU Public License
  8. *
  9. */
  10.  
  11. class lfcd
  12. {
  13.         /**
  14.         * Constant for "fail" error
  15.         */
  16.         const ERROR_FAIL = 1;
  17.  
  18.         /**
  19.         * Constant for warnings
  20.         */
  21.         const ERROR_WARNING = 2;
  22.  
  23.         /**
  24.         * Constant for notices
  25.         */
  26.         const ERROR_NOTICE = 3;
  27.  
  28.         /**
  29.         * Constant for information notices
  30.         */
  31.         const ERROR_INFO = 4;
  32.  
  33.         /**
  34.         * Constant for permission errors
  35.         */
  36.         const ERROR_PERMISSION = 5;
  37.  
  38.         /**
  39.         * Output validation report as plain text
  40.         */
  41.         const OUTPUT_TEXT = 0;
  42.  
  43.         /*
  44.         * Output validation report as BBcode
  45.         */
  46.         const OUTPUT_BBCODE = 1;
  47.  
  48.         /**
  49.         * Output validation report as HTML
  50.         */
  51.         const OUTPUT_HTML = 2;
  52.  
  53.         /**
  54.         * The language we want to check
  55.         */
  56.         private $language = '';
  57.  
  58.         /**
  59.         * The MOD files we want to check
  60.         */
  61.         private $mod_files = array();
  62.  
  63.         /**
  64.         * All phpBB files
  65.         */
  66.         private $phpbb_files = array(
  67.                 'acp/attachments',
  68.                 'acp/ban',
  69.                 'acp/board',
  70.                 'acp/bots',
  71.                 'acp/common',
  72.                 'acp/database',
  73.                 'acp/email',
  74.                 'acp/forums',
  75.                 'acp/groups',
  76.                 'acp/language',
  77.                 'acp/mods',
  78.                 'acp/modules',
  79.                 'acp/permissions',
  80.                 'acp/permissions_phpbb',
  81.                 'acp/posting',
  82.                 'acp/profile',
  83.                 'acp/prune',
  84.                 'acp/search',
  85.                 'acp/styles',
  86.                 'acp/users',
  87.                 'captcha_qa',
  88.                 'captcha_recaptcha',
  89.                 'common',
  90.                 'groups',
  91.                 'install',
  92.                 'mcp',
  93.                 'memberlist',
  94.                 'posting',
  95.                 'search',
  96.                 'ucp',
  97.                 'viewforum',
  98.                 'viewtopic',
  99.         );
  100.  
  101.         /**
  102.         * The phpBB files we ignore
  103.         */
  104.         private $ignore_phpbb_files = array(
  105.                 'help_bbcode',
  106.                 'help_faq',
  107.                 'search_ignore_words',
  108.                 'search_synonyms',
  109.         );
  110.  
  111.         /**
  112.         * Errors which were encountered during testing
  113.         */
  114.         private $errors;
  115.         private $error_files;
  116.  
  117.         /**
  118.         * Our pre-formatted PM content
  119.         */
  120.         private $message;
  121.  
  122.         public function __construct($ignore_phpbb_files = null, $language = 'en')
  123.         {
  124.                 if ($ignore_phpbb_files != null)
  125.                 {
  126.                         if (is_array($ignore_phpbb_files))
  127.                         {
  128.                                 $this->ignore_phpbb_files = $ignore_phpbb_files;
  129.                         }
  130.                         else
  131.                         {
  132.                                 $this->ignore_phpbb_files = array();
  133.                         }
  134.                 }
  135.  
  136.                 $this->language = $language;
  137.                 $this->errors = array();
  138.                 $this->error_files = array();
  139.                 $this->message = '';
  140.  
  141.                 global $phpEx, $lfcd_lang;
  142.                 include('lang.' . $phpEx);
  143.         }
  144.  
  145.         public function validate($mod_files = array())
  146.         {
  147.                 global $phpbb_root_path, $phpEx, $lfcd_lang;
  148.  
  149.                 $this->mod_files = $mod_files;
  150.                 $this->errors = array();
  151.                 $this->error_files = array();
  152.                 $this->message = '';
  153.  
  154.                 if (!file_exists($phpbb_root_path . 'language/' . $this->language . '/'))
  155.                 {
  156.                         //echo '<h3>Error: language-package "' . $this->language . '" could not be found!</h3>';
  157.                         continue;
  158.                 }
  159.  
  160.                 foreach ($this->mod_files as $mod_file)
  161.                 {
  162.                         include($phpbb_root_path . 'language/' . $this->language . '/' . $mod_file . '.' . $phpEx);
  163.                         $mod_lang = $lang;
  164.                         $lang = null;
  165.  
  166.                         foreach ($this->phpbb_files as $phpbb_file)
  167.                         {
  168.                                 if (in_array($phpbb_file, $this->ignore_phpbb_files))
  169.                                 {
  170.                                         continue;
  171.                                 }
  172.  
  173.                                 include($phpbb_root_path . 'language/' . $this->language . '/' . $phpbb_file . '.' . $phpEx);
  174.                                 $phpbb_lang = $lang;
  175.                                 $lang = null;
  176.  
  177.                                 $conflicting_keys = array_keys(array_intersect_key($mod_lang, $phpbb_lang));
  178.                                 if (!empty($conflicting_keys))
  179.                                 {
  180.                                         foreach ($conflicting_keys as $conflict)
  181.                                         {
  182.                                                 $this->push_error($mod_file, $phpbb_file, $conflict, $mod_lang, $phpbb_lang);
  183.                                         }
  184.                                 }
  185.                         }
  186.                 }
  187.         }
  188.  
  189.         private function push_error($mod_file, $phpbb_file, $conflict, $mod_lang, $phpbb_lang)
  190.         {
  191.                 global $lfcd_lang;
  192.                 $type = $this->get_error_type($mod_file, $phpbb_file);
  193.  
  194.                 switch ($type)
  195.                 {
  196.                         case self::ERROR_FAIL:
  197.                                 $this->message .= '[color=red][ [b]' . $lfcd_lang['LFCD_FAIL_RESULT'] . '[/b] ][/color] ';
  198.                         break;
  199.  
  200.                         case self::ERROR_NOTICE:
  201.                                 $this->message .= '[color=blue][ [b]' . $lfcd_lang['LFCD_NOTICE_RESULT'] . '[/b] ][/color] ';
  202.                         break;
  203.  
  204.                         case self::ERROR_WARNING:
  205.                                 $this->message .= '[color=orange][ [b]' . $lfcd_lang['LFCD_WARNING_RESULT'] . '[/b] ][/color] ';
  206.                         break;
  207.  
  208.                         case self::ERROR_INFO:
  209.                                 $this->message .= '[color=purple][ [b]' . $lfcd_lang['LFCD_INFO_RESULT'] . '[/b] ][/color] ';
  210.                         break;
  211.  
  212.                         case self::ERROR_PERMISSION:
  213.                                 $this->message .= '[color=#008080][ [b]' . $lfcd_lang['LFCD_PERMISSION_RESULT'] . '[/b] ][/color] ' . $lfcd_lang['LFCD_PERMISSION_MESSAGE'];
  214.                         break;
  215.  
  216.                         default:
  217.                                 return;
  218.                                 //$this->message .= '[color=orange][ [b]' . $lfcd_lang['LFCD_WARNING_RESULT'] . '[/b] ][/color] [b]' . $lfcd_lang['LFCD_INVALID_TYPE'] . "\n";
  219.                                 //$this->message .= '[color=purple][ [b]' . $lfcd_lang['LFCD_INFO_RESULT'] . '[/b] ][/color] ' . $message . "\n";
  220.                 }
  221.  
  222.                 if ($type != self::ERROR_PERMISSION)
  223.                 {
  224.                         $this->message .= sprintf(
  225.                                 $lfcd_lang['LFCD_CONFLICT'],
  226.                                 $mod_file,
  227.                                 $phpbb_file,
  228.                                 $conflict,
  229.                                 self::to_string($mod_lang[$conflict]),
  230.                                 self::to_string($phpbb_lang[$conflict])
  231.                         ) . "\n\n";
  232.  
  233.                         $this->errors[$type][] = array(
  234.                                 'mod_file'              => $mod_file,
  235.                                 'phpbb_file'    => $phpbb_file,
  236.                                 'conflict'              => $conflict,
  237.                                 'mod_lang'              => $mod_lang,
  238.                                 'phpbb_lang'    => $phpbb_lang,
  239.                         );
  240.                 }
  241.  
  242.                 $this->error_files[$mod_file][$type][] = array(
  243.                         'mod_file'              => $mod_file,
  244.                         'phpbb_file'    => $phpbb_file,
  245.                         'conflict'              => $conflict,
  246.                         'mod_lang'              => $mod_lang,
  247.                         'phpbb_lang'    => $phpbb_lang,
  248.                 );
  249.         }
  250.  
  251.         public function get_report($output = self::OUTPUT_BBCODE)
  252.         {
  253.                 global $lfcd_lang;
  254.                 $return = sprintf($lfcd_lang['LFCD_TESTING_FILES'], implode(",\n", $this->mod_files)) . "\n\n ";
  255.                 $return .= $lfcd_lang['LFCD_STATISTIC'] . "\n";
  256.  
  257.                 foreach ($this->error_files as $file => $errors)
  258.                 {
  259.                         if (!isset($errors[self::ERROR_PERMISSION]))
  260.                         {
  261.                                 $return .= sprintf(
  262.                                         $lfcd_lang['LFCD_STATISTIC_FILE'],
  263.                                         $file,
  264.                                         (isset($errors[self::ERROR_FAIL]) ? sizeof($errors[self::ERROR_FAIL]) : '[/color][color=black]0'),
  265.                                         (isset($errors[self::ERROR_WARNING]) ? sizeof($errors[self::ERROR_WARNING]) : '[/color][color=black]0'),
  266.                                         (isset($errors[self::ERROR_NOTICE]) ? sizeof($errors[self::ERROR_NOTICE]) : '[/color][color=black]0'),
  267.                                         (isset($errors[self::ERROR_INFO]) ? sizeof($errors[self::ERROR_INFO]) : '[/color][color=black]0')
  268.                                 ) . "\n";
  269.                         }
  270.                         else
  271.                         {
  272.                                 $return .= sprintf($lfcd_lang['LFCD_STATISTIC_PERMISSION'], $file) . "\n";
  273.                         }
  274.                 }
  275.                 $return .= "\n\n";
  276.                 $return .= $lfcd_lang['LFCD_CONFLICTS'] . "\n";
  277.  
  278.                 switch ($output)
  279.                 {
  280.                         case self::OUTPUT_BBCODE:
  281.                                 $text = htmlspecialchars($return . $this->message);
  282.                                 return self::generate_text_for_html_display($text, true);
  283.  
  284.                         case self::OUTPUT_HTML:
  285.                                 $text = htmlspecialchars($return . $this->message);
  286.                                 return self::generate_text_for_html_display($text);
  287.  
  288.                         case self::OUTPUT_TEXT:
  289.                                 $text = htmlspecialchars($return . $this->message);
  290.                                 $text = self::generate_text_for_html_display($text);
  291.                                 //$text = htmlspecialchars_decode(strip_tags(str_replace('<br />', "\n", $text)));
  292.                                 $text = str_replace("\n\n", "\n", $text);
  293.                                 $text = str_replace("\n", PHP_EOL, $text);
  294.                                 return $text;
  295.                 }
  296.         }
  297.  
  298.         private function get_error_type($mod_file, $phpbb_file)
  299.         {
  300.                 if (strpos($mod_file, 'permission') !== false && strpos($phpbb_file, 'permission') !== false)
  301.                 {
  302.                         return self::ERROR_PERMISSION;
  303.                 }
  304.  
  305.                 if ((strpos($mod_file, 'mods/info_acp_') === 0 && strpos($phpbb_file, 'acp/') === 0) ||
  306.                         (strpos($mod_file, 'mods/info_mcp_') === 0 && $phpbb_file == 'mcp') ||
  307.                         (strpos($mod_file, 'mods/info_ucp_') === 0 && $phpbb_file == 'ucp') ||
  308.                         (strpos($mod_file, 'acp') !== false && $phpbb_file == 'acp/common') ||
  309.                         ($phpbb_file == 'common'))
  310.                 {
  311.                         return self::ERROR_FAIL;
  312.                 }
  313.  
  314.                 if ((strpos($mod_file, 'acp') === false && strpos($phpbb_file, 'acp/') === 0))
  315.                 {
  316.                         return self::ERROR_NOTICE;
  317.                 }
  318.  
  319.                 if (strpos($mod_file, 'install') || strpos($phpbb_file, 'install'))
  320.                 {
  321.                         return self::ERROR_INFO;
  322.                 }
  323.  
  324.                 return self::ERROR_WARNING;
  325.         }
  326.  
  327.         /**
  328.         * Display validation results as HTML
  329.         */
  330.         static public function generate_text_for_html_display($text, $soft = false)
  331.         {
  332.                 //Replace new
  333.                 $text = str_replace("\n", "<br />\n", $text);
  334.                 if ($soft)
  335.                 {
  336.                         return $text;
  337.                 }
  338.  
  339.                 //BBCode replacement array
  340.                 $bbcode = array(
  341.                         "/\[b\](.*?)\[\/b\]/is" => '<span style="font-weight:bold;">$1</span>',
  342.                         "/\[u\](.*?)\[\/u\]/is" => '<span style="text-decoration:underline;">$1</span>',
  343.                         "/\[color\=(.*?)\](.*?)\[\/color\]/is" => '<span style="color:$1;">$2</span>',
  344.                         "/\[code\](.*?)\[\/code\]/is" => '<pre style="padding-left:20px;">$1</pre>',
  345.                         '#\[url(=(.*))?\](.*)\[/url\]#iUe' => "validate_url('\$2', '\$3')",
  346.                         "/\[size\=(.*?)\](.*?)\[\/size\]/is" => '<span style="font-size: $1%;">$2</span>',
  347.                 );
  348.  
  349.                 //Replace BBCode
  350.                 $text = preg_replace(array_keys($bbcode), array_values($bbcode), $text);
  351.  
  352.                 return $text;
  353.         }
  354.  
  355.         /**
  356.         * Convert the language entry to a beautiful string
  357.         */
  358.         static public function to_string($mixed, $depth = 0)
  359.         {
  360.                 if (!is_array($mixed))
  361.                 {
  362.                         return /*str_repeat("\t", $depth) . */$mixed . "\n";
  363.                 }
  364.                 $return = str_repeat("\t", $depth) . "Array(\n";
  365.                 foreach ($mixed as $key => $value)
  366.                 {
  367.                         $return .= str_repeat("\t", $depth + 1) . $key . ' => ' . self::to_string($value, $depth + 1);
  368.                 }
  369.                 return $return . str_repeat("\t", $depth) . ")\n";
  370.         }
  371. }