= filemtime($filename)) { include($cacheDir . $cacheName); return $Data; } $f = file_get_contents($filename); $Data = array(); $key = false; $f = str_replace("\r\n", "\n", $f); $f = str_replace("\r", "\n", $f); $f = split("\n", $f); while (count($f)) { $line = trim(GetLinePSR($f)); if (substr($line, 0, 1) == '*') { $key = trim(substr($line, 1)); if (isset($Data[$key])) return array("Key $KEY is defined twice. PSR file is incorrect."); $Data[$key] = array(); } elseif ($line != '') { $line = str_replace("\\n", "\n", $line); if ($key === false) return array("Problem loading data. PSR file is incorrect."); $Chance = 1; if (preg_match("/^([0-9]+):(.*)\$/m", $line, $hits)) { $Chance = $hits[1]; $line = $hits[2]; } $Data[$key][] = array($Chance, $line); } } if ($doCache) { $cache = " $rules) { $Total = 0; foreach ($rules as $index => $data) { $Total += $data[0]; $Data[$key][$index][0] = $Total; } $Totals[$key] = $Total; } // Generate the message $Results = array(); $StartRule = array_keys($Data); $StartRule = $StartRule[0]; while ($count --) { $R = GenerateRulePSR($Data, $Totals, $StartRule); $Results[] = $R; } return $Results; } function GetLinePSR(&$data) { $line = trim(array_shift($data)); // Ignore comments while (substr($line, 0, 1) == '#') { if (count($data)) $line = trim(array_shift($data)); else $line = ''; } while (substr($line, -1) == '\\') { if (count($data)) { $line2 = trim(array_shift($data)); while ($line2[0] == '#') { if (count($data)) $line2 = trim(array_shift($data)); else $line2 = ''; } } else $line2 = ''; $line = substr($line, 0, strlen($line) - 1) . $line2; } return $line; } function GenerateRulePSR(&$Data, &$Totals, $Key) { // Pick a random rule $Text = PickRandomPSR($Totals[$Key], $Data[$Key]); $bits = preg_split('/\\[([\\^]?[^\\]]*)\\]/', $Text, -1, PREG_SPLIT_DELIM_CAPTURE); $out = array_shift($bits); while (count($bits)) { $Rule = array_shift($bits); $MakeUpper = false; if (substr($Rule, 0, 1) == '^') { $MakeUpper = true; $Rule = substr($Rule, 1); } if ($Rule == '[' || $Rule == ']' || $Rule == '*' || $Rule == '#') { $out .= $Rule; } elseif (! isset($Data[$Rule])) { $out .= "[Rule \"$Rule\" not found.]"; } else { $out2 = GenerateRulePSR($Data, $Totals, $Rule); if ($MakeUpper) $out2 = ucfirst($out2); $out .= $out2; } $out .= array_shift($bits); } return $out; } function PickRandomPSR($total, $arr) { // $arr is an array of ($num, $val) // $total is the biggest ($num) // Pick a number from 1 to $total $num = rand(1, $total); // Now find where it is in the array $i = count($arr) / 2; settype($i, 'integer'); $j = $i / 2; settype($j, 'integer'); while ($j > 2) { if ($arr[$i][0] > $num) { $i -= $j; } else { $i += $j; } $j = $j / 2; settype($j, 'integer'); } while ($arr[$i][0] < $num && isset($arr[$i + 1])) { $i ++; } while (isset($arr[$i - 1]) && $arr[$i - 1][0] >= $num) $i --; return $arr[$i][1]; }