Exemplo n.º 1
0
 private static function _pack_analyze($script, $regexp, $encode)
 {
     $wall = array();
     // all words in the script
     // get all words
     preg_match_all($regexp, $script, $wall);
     // simulate the javascript comportement of global match
     $wall = $wall[0];
     if (empty($wall)) {
         return false;
     }
     $wsort = array();
     // list of words sorted by frequency
     $wusrt = array();
     // list of words without sorting
     $wprot = array();
     // instances of "protected" words
     $wenco = array();
     // dictionary of word->encoding
     $dprot = array();
     // dictionary of word->"word"
     $value = array();
     // dictionary of charCode->encoding (eg. 256->ff)
     self::$_PWC = array();
     // word->count
     $i = count($wall);
     $j = 0;
     //$word = null;
     // count the occurrences - used for sorting later
     do {
         --$i;
         $word = '$' . $wall[$i];
         if (!isset(self::$_PWC[$word])) {
             self::$_PWC[$word] = 0;
             $wusrt[$j] = $word;
             // make a dictionary of all of the protected words in this script
             //  these are words that might be mistaken for encoding
             $value[$j] = call_user_func(array('self', $encode), $j);
             $dprot['$' . $value[$j]] = $j++;
         }
         // increment the word counter
         self::$_PWC[$word]++;
     } while ($i > 0);
     // prepare to sort the word list, first we must protect
     //  words that are also used as codes. we assign them a code
     //  equivalent to the word itself.
     // e.g. if "do" falls within our encoding range
     //      then we store keywords["do"] = "do";
     // this avoids problems when decoding
     $i = count($wusrt);
     do {
         $word = $wusrt[--$i];
         if (isset($dprot[$word])) {
             $wsort[$dprot[$word]] = substr($word, 1);
             $iprot[$dprot[$word]] = true;
             self::$_PWC[$word] = 0;
         }
     } while ($i);
     // sort the words by frequency
     // Note: the javascript and php version of sort can be different :
     // in php manual, usort :
     // " If two members compare as equal,
     // their order in the sorted array is undefined."
     // so the final packed script is different of the Dean's javascript version
     // but equivalent.
     // the ECMAscript standard does not guarantee this behaviour,
     // and thus not all browsers (e.g. Mozilla versions dating back to at
     // least 2003) respect this.
     usort($wusrt, array('self', '_pack_sortwords'));
     $j = 0;
     // because there are "protected" words in the list
     //  we must add the sorted words around them
     do {
         if (!isset($wsort[$i])) {
             $wsort[$i] = substr($wusrt[$j++], 1);
         }
         $wenco[$wsort[$i]] = $value[$i];
     } while (++$i < count($wusrt));
     return array('sorted' => $wsort, 'encoded' => $wenco, 'protected' => $wprot);
 }