Advertisement
Guest User

Untitled

a guest
Jan 24th, 2010
212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 14.29 KB | None | 0 0
  1. <?php
  2.  
  3. /*
  4. +---------------------------------------------------------------------------+
  5. | OpenX v2.8                                                                |
  6. | ==========                                                                |
  7. |                                                                           |
  8. | Copyright (c) 2003-2009 OpenX Limited                                     |
  9. | For contact details, see: http://www.openx.org/                           |
  10. |                                                                           |
  11. | This program is free software; you can redistribute it and/or modify      |
  12. | it under the terms of the GNU General Public License as published by      |
  13. | the Free Software Foundation; either version 2 of the License, or         |
  14. | (at your option) any later version.                                       |
  15. |                                                                           |
  16. | This program is distributed in the hope that it will be useful,           |
  17. | but WITHOUT ANY WARRANTY; without even the implied warranty of            |
  18. | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the             |
  19. | GNU General Public License for more details.                              |
  20. |                                                                           |
  21. | You should have received a copy of the GNU General Public License         |
  22. | along with this program; if not, write to the Free Software               |
  23. | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA |
  24. +---------------------------------------------------------------------------+
  25. $Id: lib-swf.inc.php 47749 2009-12-23 21:08:35Z post_commit $
  26. */
  27.  
  28. // Define SWF tags
  29. define ('swf_tag_identify',          "FWS");
  30. define ('swf_tag_compressed',        "CWS");
  31. define ('swf_tag_geturl',            "\x83");
  32. define ('swf_tag_null',              "\x00");
  33. define ('swf_tag_actionpush',        "\x96");
  34. define ('swf_tag_actiongetvariable', "\x1C");
  35. define ('swf_tag_actiongeturl2',     "\x9A\x01");
  36. define ('swf_tag_actiongetmember',   "\x4E");
  37.  
  38.  
  39. // Define preferences
  40. $GLOBALS['swf_variable']   = 'alink';       // The name of the ActionScript variable used for urls
  41. $GLOBALS['swf_target_var'] = 'atar';        // The name of the ActionScript variable used for targets
  42.  
  43.  
  44.  
  45. /*-------------------------------------------------------*/
  46. /* Get the Flash version of the banner                   */
  47. /*-------------------------------------------------------*/
  48.  
  49. function phpAds_SWFVersion($buffer)
  50. {
  51.     if (substr($buffer, 0, 3) == swf_tag_identify ||
  52.         substr($buffer, 0, 3) == swf_tag_compressed)
  53.         return ord(substr($buffer, 3, 1));
  54.     else
  55.         return false;
  56. }
  57.  
  58.  
  59.  
  60. /*-------------------------------------------------------*/
  61. /* Is the Flash file compressed?                         */
  62. /*-------------------------------------------------------*/
  63.  
  64. function phpAds_SWFCompressed($buffer)
  65. {
  66.     if (substr($buffer, 0, 3) == swf_tag_compressed)
  67.         return true;
  68.     else
  69.         return false;
  70. }
  71.  
  72.  
  73.  
  74. /*-------------------------------------------------------*/
  75. /* Compress Flash file                                   */
  76. /*-------------------------------------------------------*/
  77.  
  78. function phpAds_SWFCompress($buffer)
  79. {
  80.     $version = ord(substr($buffer, 3, 1));
  81.  
  82.     if (function_exists('gzcompress') &&
  83.         substr($buffer, 0, 3) == swf_tag_identify &&
  84.         $version >= 3)
  85.     {
  86.         // When compressing an old file, update
  87.         // version, otherwise keep existing version
  88.         if ($version < 6) $version = 6;
  89.  
  90.         $output  = 'C';
  91.         $output .= substr ($buffer, 1, 2);
  92.         $output .= chr($version);
  93.         $output .= substr ($buffer, 4, 4);
  94.         $output .= gzcompress (substr ($buffer, 8));
  95.  
  96.         return ($output);
  97.     }
  98.     else
  99.         return ($buffer);
  100. }
  101.  
  102.  
  103.  
  104. /*-------------------------------------------------------*/
  105. /* Decompress Flash file                                 */
  106. /*-------------------------------------------------------*/
  107.  
  108. function phpAds_SWFDecompress($buffer)
  109. {
  110.     if (function_exists('gzuncompress') &&
  111.         substr($buffer, 0, 3) == swf_tag_compressed &&
  112.         ord(substr($buffer, 3, 1)) >= 6)
  113.     {
  114.         $output  = 'F';
  115.         $output .= substr ($buffer, 1, 7);
  116.         $output .= gzuncompress (substr ($buffer, 8));
  117.  
  118.         return ($output);
  119.     }
  120.     else
  121.         return ($buffer);
  122. }
  123.  
  124.  
  125.  
  126. /*-------------------------------------------------------*/
  127. /* Upgrade version of a Flash file                       */
  128. /*-------------------------------------------------------*/
  129.  
  130. function phpAds_SWFUpgrade($buffer)
  131. {
  132.     $version = ord(substr($buffer, 3, 1));
  133.  
  134.     if ($version < 5)
  135.     {
  136.          $version = 5;
  137.  
  138.         $output = substr ($buffer, 0, 3);
  139.         $output .= chr($version);
  140.         $output .= substr ($buffer, 4, 4);
  141.         $output .= substr ($buffer, 8);
  142.  
  143.         return ($output);
  144.     }
  145.     else
  146.         return ($buffer);
  147. }
  148.  
  149.  
  150.  
  151. /*-------------------------------------------------------*/
  152. /* Get the dimensions of the Flash banner                */
  153. /*-------------------------------------------------------*/
  154.  
  155. function phpAds_SWFBits($buffer, $pos, $count)
  156. {
  157.     $result = 0;
  158.  
  159.     for ($loop = $pos; $loop < $pos + $count; $loop++)
  160.         $result = $result + ((((ord($buffer[(int)($loop / 8)])) >> (7 - ($loop % 8))) & 0x01) << ($count - ($loop - $pos) - 1));
  161.  
  162.     return $result;
  163. }
  164.  
  165. function phpAds_SWFDimensions($buffer)
  166. {
  167.     // Decompress if file is a Flash MX compressed file
  168.     if (phpAds_SWFCompressed($buffer))
  169.         $buffer = phpAds_SWFDecompress($buffer);
  170.  
  171.     // Get size of rect structure
  172.     $bits   = phpAds_SWFBits ($buffer, 64, 5);
  173.  
  174.     // Get rect
  175.     $width  = (int)((phpAds_SWFBits ($buffer, 69 + $bits, $bits) - phpAds_SWFBits ($buffer, 69, $bits)) / 20);
  176.     $height = (int)((phpAds_SWFBits ($buffer, 69 + (3 * $bits), $bits) - phpAds_SWFBits ($buffer, 69 + (2 * $bits), $bits)) / 20);
  177.  
  178.  
  179.     return (
  180.         array($width, $height)
  181.     );
  182. }
  183.  
  184.  
  185.  
  186. /*-------------------------------------------------------*/
  187. /* Get info about the hardcoded urls                     */
  188. /*-------------------------------------------------------*/
  189.  
  190. function phpAds_SWFInfo($buffer)
  191. {
  192.     global $swf_variable, $swf_target_var;
  193.  
  194.     // Decompress if file is a Flash MX compressed file
  195.     if (phpAds_SWFCompressed($buffer))
  196.         $buffer = phpAds_SWFDecompress($buffer);
  197.  
  198.     $parameters = array();
  199.     $linkcount = 1;
  200.  
  201.     while (preg_match('/                                # begin
  202.                             (?<=\x00)                   # match if preceded by a swf_tag_null
  203.                             (?:
  204.                                 \x83                        # match a swf_tag_geturl
  205.                                 ..                          # match a UI16  (combined length)
  206.                                 (
  207.                                     (?:https?:\/\/|javascript:).+?      # match the url
  208.                                 )
  209.                                 \x00                        # match a swf_tag_null
  210.                                 (
  211.                                     .*?                     # match the target
  212.                                 )
  213.                                 \x00                        # match a swf_tag_null
  214.                             |                           # or
  215.                                 \x96                        # match a swf_tag_actionpush
  216.                                 ..                          # match a U16 word (length)
  217.                                 \x00                        # match a swf_tag_null
  218.                                 (
  219.                                     (?:https?:\/\/|javascript:).+?      # match the url
  220.                                 )
  221.                                 \x00                        # match a swf_tag_null
  222.                                 \x96                        # match a swf_tag_actionpush
  223.                                 ..                          # match a UI16 (length)
  224.                                 \x00                        # match a swf_tag_null
  225.                                 (
  226.                                     .*?                     # match the target
  227.                                 )
  228.                                 \x00                        # match a swf_tag_null
  229.                                 \x9A\x01                    # match a swf_tag_geturl2
  230.                             )
  231.                         /sx', $buffer, $m))
  232.     {
  233.         if ($m[0]{0} == chr(0x83))
  234.         {
  235.             $parameter_url = $m[1];
  236.             $parameter_target = $m[2];
  237.         }
  238.         else
  239.         {
  240.             $parameter_url = $m[3];
  241.             $parameter_target = $m[4];
  242.         }
  243.  
  244.         $parameters[$linkcount] = array(
  245.             $parameter_url, $parameter_target
  246.         );
  247.  
  248.         $buffer = str_replace($m[0], '', $buffer);
  249.  
  250.         $linkcount++;
  251.     }
  252.  
  253.     if (count($parameters))
  254.         return ($parameters);
  255.     else
  256.         return false;
  257. }
  258.  
  259.  
  260.  
  261. /*-------------------------------------------------------*/
  262. /* Convert hard coded urls                               */
  263. /*-------------------------------------------------------*/
  264.  
  265. function phpAds_SWFConvert($buffer, $compress, $allowed)
  266. {
  267.     global $swf_variable, $swf_target_var;
  268.  
  269.     // Decompress if file is a Flash MX compressed file
  270.     if (phpAds_SWFCompressed($buffer))
  271.         $buffer = phpAds_SWFDecompress($buffer);
  272.  
  273.  
  274.     $parameters = array();
  275.     $linkcount = 1;
  276.     $allowedcount = 1;
  277.     $final = $buffer;
  278.  
  279.     $masked_parts = array();
  280.  
  281.     while (preg_match('/                                # begin
  282.                             ^
  283.                             (
  284.                             .+?                         # match anything from the start
  285.                             \x00                        # match a swf_tag_null
  286.                             )
  287.                             (
  288.                                 \x83                        # match a swf_tag_geturl
  289.                                 ..                          # match a UI16  (combined length)
  290.                                 (
  291.                                     (?:https?:\/\/|javascript:).+?      # match the url
  292.                                 )
  293.                                 \x00                        # match a swf_tag_null
  294.                                 (
  295.                                     .*?                     # match the target
  296.                                 )
  297.                                 \x00                        # match a swf_tag_null
  298.                             |                           # or
  299.                                 \x96                        # match a swf_tag_actionpush
  300.                                 ..                          # match a U16 word (length)
  301.                                 \x00                        # match a swf_tag_null
  302.                                 (
  303.                                     (?:https?:\/\/|javascript:).+?      # match the url
  304.                                 )
  305.                                 \x00                        # match a swf_tag_null
  306.                                 \x96                        # match a swf_tag_actionpush
  307.                                 ..                          # match a UI16 (length)
  308.                                 \x00                        # match a swf_tag_null
  309.                                 (
  310.                                     .*?                     # match the target
  311.                                 )
  312.                                 \x00                        # match a swf_tag_null
  313.                                 \x9A\x01                    # match a swf_tag_geturl2
  314.                             )
  315.                         /sx', $buffer, $m))
  316.     {
  317.         $geturl_part    = $m[2];
  318.         $previous_part  = $m[1];
  319.  
  320.         // Replace masked parts with actual content
  321.         if (count($masked_parts)) {
  322.             $previous_part = strtr($previous_part, array_flip($masked_parts));
  323.         }
  324.  
  325.         $allowed_types = array(12, 26, 34); // DoAction, PlaceObject2, DefineButton2
  326.         $original = '';
  327.         for ($len = 2; $len < strlen($previous_part); $len++)
  328.         {
  329.             $recordheader = substr($previous_part, -$len);
  330.             $object_tag = substr($recordheader, 0, 2);
  331.             $expected_len = strlen($geturl_part) + $len;
  332.  
  333.             $tag_type = (ord($object_tag{1}) << 2 | (ord($object_tag{0}) & ~0x3F) >> 6) & 0xFF;
  334.  
  335.             if (!in_array($tag_type, $allowed_types))
  336.                 continue;
  337.  
  338.             // Check for long RECORDHEADER
  339.             if ($len > 6 && (ord($object_tag{0}) & 0x3F) == 0x3F)
  340.             {
  341.                 $object_extended = true;
  342.                 $object_len = unpack('V', substr($recordheader, 2, 4));
  343.                 $object_len = current($object_len);
  344.                 $expected_len -= 6;
  345.             }
  346.             else
  347.             {
  348.                 $object_extended = false;
  349.                 $object_len = ord($object_tag{0}) & 0x3F;
  350.                 $expected_len -= 2;
  351.             }
  352.  
  353.             if ($object_len >= $expected_len)
  354.             {
  355.                 $replacement = $original = $recordheader.$geturl_part;
  356.  
  357.                 break;
  358.             }
  359.         }
  360.  
  361.         if (!strlen($original))
  362.             die("Error: unsupported tag");
  363.  
  364.         $geturl2_part = swf_tag_actionpush.
  365.                             chr(strlen('_root')+2).
  366.                             swf_tag_null.
  367.                         swf_tag_null.
  368.                             '_root'.
  369.                         swf_tag_null.
  370.  
  371.                         swf_tag_actiongetvariable.
  372.  
  373.                         swf_tag_actionpush.
  374.                             chr(strlen($swf_variable.$linkcount)+2).
  375.                             swf_tag_null.
  376.                         swf_tag_null.
  377.                             $swf_variable.
  378.                             $linkcount.
  379.                         swf_tag_null.
  380.  
  381.                         swf_tag_actiongetmember.
  382.  
  383.                         swf_tag_actionpush.
  384.                             chr(strlen('_root')+2).
  385.                             swf_tag_null.
  386.                         swf_tag_null.
  387.                             '_root'.
  388.                         swf_tag_null.
  389.  
  390.                         swf_tag_actiongetvariable.
  391.  
  392.                         swf_tag_actionpush.
  393.                             chr(strlen($swf_target_var.$linkcount)+2).
  394.                             swf_tag_null.
  395.                         swf_tag_null.
  396.                             $swf_target_var.
  397.                             $linkcount.
  398.                         swf_tag_null.
  399.  
  400.                         swf_tag_actiongetmember.
  401.  
  402.                         swf_tag_actiongeturl2.
  403.  
  404.                         swf_tag_null.
  405.  
  406.                         swf_tag_null;
  407.  
  408.         // Check for functions (ActionDefineFunction)
  409.         if (preg_match('/^.*(\x9B(..).*?)(..)$/s', $recordheader, $m)) {
  410.             $fheader_len = unpack('v', $m[2]);
  411.             $fheader_len = current($fheader_len);
  412.             $fbody_len = unpack('v', $m[3]);
  413.             $fbody_len = current($fbody_len);
  414.             if ($fheader_len == strlen($m[1]) - 1)
  415.             {
  416.                 // getURL is inside an ActionDefineFunction
  417.                 $fbody_len += strlen($geturl2_part) - strlen($geturl_part);
  418.                 $geturl_part    = $m[1].$m[3].$geturl_part;
  419.                 $geturl2_part   = $m[1].pack('v', $fbody_len).$geturl2_part;
  420.             }
  421.         }
  422.  
  423.         $replacement = str_replace($geturl_part, $geturl2_part, $replacement);
  424.  
  425.         $object_len2 = $object_len + strlen($geturl2_part) - strlen($geturl_part);
  426.  
  427.         $replacement = substr($replacement, $object_extended ? 6 : 2);
  428.  
  429.         if ($object_len2 < 0x3F)
  430.             $replacement = chr(($object_tag{0} && 0xC0) | $object_len2).$object_tag{1}.$replacement;
  431.         else
  432.             $replacement = chr(ord($object_tag{0}) | 0x3F).$object_tag{1}.pack('V', $object_len2).$replacement;
  433.  
  434.         // Check for DefineSprite
  435.         $definesprite_part = substr($previous_part, -strlen($recordheader) - 10, 10);
  436.         if (preg_match('/^\xFF\x09(....)(....)$/s', $definesprite_part, $m)) {
  437.             // Long DefineSprite recordheader
  438.             $object_len = unpack('V', $m[1]);
  439.             $object_len = current($object_len);
  440.  
  441.             $object_tag = substr($definesprite_part, 0, 2);
  442.  
  443.             $object_len += strlen($geturl2_part) - strlen($geturl_part);
  444.  
  445.             $original = $definesprite_part.$original;
  446.  
  447.             $replacement = chr(ord($object_tag{0}) | 0x3F).$object_tag{1}.pack('V', $object_len).$m[2].$replacement;
  448.         }
  449.  
  450.         // Is this link allowed to be converted?
  451.         if (in_array($allowedcount, $allowed))
  452.         {
  453.             // Convert
  454.             $final = str_replace($original, $replacement, $final);
  455.  
  456.             // Fix file size
  457.             $file_size = unpack('V', substr($final, 4, 4));
  458.             $file_size = current($file_size) + strlen($replacement) - strlen($original);
  459.  
  460.             $final = substr($final, 0, 4).pack('V', $file_size).substr($final, 8);
  461.  
  462.             $parameters[$linkcount] = $allowedcount;
  463.  
  464.             $linkcount++;
  465.         } else {
  466.             $mask = '^'.pack('v', $allowedcount).'|'.str_pad('', strlen($geturl_part) - 5, "\xDE\xAD\xBE\xEF").'$';
  467.  
  468.             $masked_parts[$geturl_part] = $mask;
  469.         }
  470.  
  471.         $allowedcount++;
  472.  
  473.         $buffer = strtr($final, $masked_parts);
  474.     }
  475.  
  476.  
  477.     if ($compress == true)
  478.         $final = phpAds_SWFCompress($final);
  479.     else
  480.         $final = phpAds_SWFUpgrade($final);
  481.  
  482.     return (array($final, $parameters));
  483. }
  484.  
  485. // Debug function
  486. function hex_dump($str)
  487. {
  488.     echo "<pre>";
  489.     $len = strlen($str);
  490.  
  491.     $buf = '';
  492.     for ($i=0; $i<$len;$i++) {
  493.         $buf .= sprintf('%02X', ord(substr($str, $i, 1)));
  494.     }
  495.  
  496.     $buf = explode(' ', chunk_split($buf, 8, ' '));
  497.  
  498.     for ($i = 0; $i<$len; $i += 32) {
  499.         $hex = '';
  500.         for ($j = 0; $j < 8; $j++) {
  501.             if ($chunk = array_shift($buf)) {
  502.                 $hex .= $chunk.' ';
  503.             }
  504.         }
  505.  
  506.         printf("%-72s   %s\n", $hex, preg_replace('/[\x00-\x1F\x80-\xFF]/', '.', substr($str, $i, 32)));
  507.     }
  508.  
  509.     echo "\n</pre>";
  510.     flush();
  511. }
  512.  
  513.  
  514. ?>
  515.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement