home *** CD-ROM | disk | FTP | other *** search
/ Hackers Magazine 57 / CdHackersMagazineNr57.iso / Software / Programming / dhtmlxMenu.exe / dhtmlxMenu / libCompiler / images.php < prev    next >
Encoding:
PHP Script  |  2009-10-13  |  8.2 KB  |  253 lines

  1. <?php
  2. function copyImgsFiles($source,$dest,$skin)
  3. if (!is_dir($source)) return;
  4. $folder = opendir($source);
  5. while($file = readdir($folder))
  6. {
  7. if ($file == '.' || $file == '..') {
  8.            continue;
  9.        }
  10.        if(is_dir($source.$file))
  11.        {
  12.                if ($file!=".svn" && (strpos($file,"dhx")===false || preg_match("#(dhx|skin)_".$skin."#i",$file)==1)){
  13.                    @mkdir($dest.$file);
  14.                    copyImgsFiles($source.$file."/",$dest.$file."/",$skin);
  15.                }
  16.                    
  17.        }
  18.        else 
  19.        {
  20.        //echo $source.$file;
  21.         copy($source.$file,$dest.$file);
  22.        }
  23. }
  24. closedir($folder);
  25. return 1;
  26. }
  27.  
  28. function export_images($component,$skin){
  29.     $name=time();
  30.     while (file_exists("./export/".$name)){
  31.         $name+=1;
  32.     }
  33.  
  34.     $name="./export/".$name;
  35.     
  36.     mkdir($name);
  37.     mkdir($name."/imgs");
  38.     mkdir($name."/types");
  39.     $skin=explode("_",$skin);
  40.     
  41.     for ($i=0; $i < sizeof($component); $i++) { 
  42.         copyImgsFiles("../".$component[$i]."/codebase/imgs/",$name."/imgs/",$skin[1]);
  43.         copyImgsFiles("../".$component[$i]."/codebase/types/",$name."/types/",$skin[1]);
  44.     }
  45.     return $name;
  46. };
  47.  
  48.  
  49.     define("YUI",true);
  50.  
  51. $js_header="/*
  52. Copyright DHTMLX LTD. http://www.dhtmlx.com
  53. To use this library please contact sales@dhtmlx.com to obtain license
  54. */\n";
  55.     
  56.     function replaceComments($data){
  57.         $data=str_replace('("//',"___XPATH_HEAD___",$data);
  58.         $data=str_replace('("/*',"___XPATH_HEAD2___",$data);
  59.         $data=preg_replace("#\/\/[^\r\n]*(\r\n|\n)#","\n",$data);
  60.         $data=preg_replace("#\/\*.*?\*\/#s","",$data);
  61.         $data=str_replace("___XPATH_HEAD___",'("//',$data);
  62.         $data=str_replace("___XPATH_HEAD2___",'("/*',$data);
  63.         return $data;
  64.     }
  65.     function replaceWhitespaces($data){
  66.         $data=preg_replace("|[ \f\t]+|"," ",$data);
  67.         $data=preg_replace("|\}[ \r\n]+|","};",$data);
  68.         $data=preg_replace("#((if|for|while)[\s]*\([^\)]*\))[ \r\n]+#","\\1",$data);
  69.         $data=preg_replace("|([;\{]{1})[ \r\n]+([^ \r\n]{1})|","\\1\\2",$data);
  70.         $data=str_replace(";}","}",$data); //object notation fix
  71.         $data=str_replace("};else","}else",$data); //object notation fix
  72.         $data=str_replace("};catch","}catch",$data); //object notation fix
  73.         $data=str_replace(";)",")",$data); //object notation fix
  74.         $data=str_replace(";,",",",$data); //object notation fix
  75.         $data=str_replace(";:",":",$data); //object notation fix
  76.         $data=str_replace("};]","}]",$data); //object notation fix
  77.         return $data;
  78.     }
  79.     
  80.     function getTokens($data,$name){
  81.     
  82.         preg_match_all("|//#([^\n]*)|",$data,$chunks,PREG_OFFSET_CAPTURE);
  83.             $ballans=0;
  84.             $c_size=0;
  85.             $temp=array();
  86.         for ($i=0; $i<sizeof($chunks[0]); $i++){
  87.             $s_l=$chunks[1][$i][0];
  88.             if (strpos($s_l,"{")!==false){
  89.                //start of chunk
  90.                $temps=explode(":",trim(str_replace("{","",str_replace("//#","",$s_l))));
  91.                if (sizeof($temps)!=2)
  92.                   die("Incorrect Injection found <br/>".$s_l."( position ".$chunks[0][$i][1].") at ".$name);
  93.                $temp[$c_size]=array($temps[0],$temps[1],$chunks[0][$i][1],0);
  94.                $c_size++;
  95.             }
  96.             else
  97.             {
  98.                if (!$temp[$ballans])
  99.                {
  100.                    echo "<pre>";print_r($temp);
  101.                   die("Incorrect Injection layout,  <br/> ballans corrupted. <br/>".$s_l."( position ".$chunks[0][$i][1].") at ".$name);
  102.                   return 0;
  103.                }
  104.  
  105.                for ($ii=$c_size-1; $ii>=0; $ii--)
  106.                   if ($temp[$ii][3]==0)
  107.                   {
  108.                      $temp[$ii][3]=$chunks[0][$i][1]+strlen($chunks[0][$i][0]);
  109.                      break;
  110.                   }
  111.  
  112.                $ballans++;
  113.                //end of chunk
  114.             }
  115.  
  116.          }
  117.  
  118.          if ($ballans!=(sizeof($chunks[0])/2))
  119.          {
  120.             die("Incorrect Injection layout,  <br/> ballans corrupted at ".$name);
  121.             return 0;
  122.          }
  123.  
  124.         $tokens=array();
  125.         
  126.         foreach ($temp as $k=>$v){
  127.             $name=$v[0];
  128.             if (!$tokens[$name])
  129.                $tokens[$name]=array();
  130.  
  131.             $tokens[$name][]=$v;
  132.          }
  133.          return $tokens;
  134.     }
  135.     function clearAreas($data,$tokens,$preserve){
  136.          foreach($tokens as $k=>$v){  
  137.              if ($v && array_search($k,$preserve)===FALSE){ 
  138.                 foreach($v as $kd=>$vd){
  139.                     $size=$vd[3]-$vd[2];
  140.                     $bik=str_repeat("#",$size);
  141.                     $data=substr($data,0,$vd[2]).$bik.substr($data,$vd[3]);
  142.                 }
  143.             }
  144.          }
  145.          $data=preg_replace("|(#){6,}|","",$data);
  146.          return $data;
  147.    }
  148.    
  149.     function process_request($files, $chunks, $skin, $yui=false){
  150.             
  151.         $files=explode(";",$files);
  152.         $chunks=explode(";",$chunks);
  153.         array_push($chunks,"__pro_feature");
  154.  
  155.         $js_list=array("./dhtmlxcommon.js");
  156.         $css_list=array();
  157.         $manifest = array("Skin: ".$skin);
  158.         
  159.         for ($i=0; $i<sizeof($files); $i++){
  160.             if ($files[$i]=="") continue;
  161.             if (preg_match("/.*\.js$/",$files[$i]))
  162.                 array_push($js_list,".".$files[$i]);
  163.             else
  164.                 array_push($css_list,".".$files[$i]);
  165.             }
  166.                     
  167.         
  168.         $js_list=array_values(array_unique($js_list));
  169.         $css_list=array_values(array_unique($css_list));
  170.         $chunks=array_values(array_unique($chunks));
  171.         
  172.         $components=array();
  173.         for ($i=0; $i < sizeof($js_list); $i++) { 
  174.             $temp=explode("/",$js_list[$i]);
  175.             if (sizeof($temp)>2)
  176.                 array_push($components,$temp[1]);
  177.         }
  178.         $location=export_images(array_values(array_unique($components)),$skin);
  179.         for ($i=0; $i < sizeof($components); $i++) { 
  180.             $temp_name="../".$components[$i]."/codebase/skins/".strtolower($components[$i])."_".$skin.".css";
  181.             
  182.             if (file_exists($temp_name))
  183.                 array_push($css_list,$temp_name);
  184.         }
  185.         
  186.         $css_list=array_values(array_unique($css_list));
  187.         
  188.         $js_code="";
  189.         $css_code="";
  190.         array_push($manifest,"\n======== JS CODE =========");
  191.         for ($i=0; $i<sizeof($js_list); $i++){
  192.             $check_path=str_replace("codebase","sources",$js_list[$i]);
  193.             if (is_file($check_path)) 
  194.                 $js_code.="\r\n".file_get_contents($check_path);
  195.             else{
  196.                 if (!is_file($js_list[$i])) continue;
  197.                 $js_code.="\r\n".file_get_contents($js_list[$i]);
  198.             }
  199.             array_push($manifest,$js_list[$i]);
  200.         }
  201.         array_push($manifest,"\n======== CSS CODE ========");    
  202.         for ($i=0; $i<sizeof($css_list); $i++){
  203.             if (!is_file($css_list[$i])) continue;
  204.             $css_code.="\r\n".file_get_contents($css_list[$i]);
  205.             array_push($manifest,$css_list[$i]);
  206.         }
  207.         
  208.         $js_code=clearAreas($js_code,getTokens($js_code,"some file"),$chunks);
  209.         $js_code.="\ndhtmlx.skin='".$skin."';";
  210.         if (!$yui)
  211.             $js_code=replaceWhitespaces(replaceComments($js_code));
  212.         else{
  213.             file_put_contents($location."/temp.js",$js_code);
  214.             `java  -jar ./yui/yuicompressor-2.3.5/build/yuicompressor-2.3.5.jar {$location}/temp.js > {$location}/temp2.js`;
  215.             $js_code=file_get_contents($location."/temp2.js");
  216.             unlink($location."/temp.js");
  217.             unlink($location."/temp2.js");
  218.         }
  219.             
  220.         $css_code=preg_replace('/"/',"'",$css_code);
  221.         $css_code=str_replace("../imgs","imgs/",$css_code);
  222.         $css_code=str_replace("../../codebase/","",$css_code);
  223.     
  224.        if (!$yui){
  225.                $css_code=preg_replace("#\/\*([^\*]|\*[^\/])*\*\/#","",$css_code);
  226.             $css_code=preg_replace('/[ \t]+/'," ",$css_code);
  227.             $css_code=preg_replace("/\{[\r\n]+/","{",$css_code);
  228.             $css_code=preg_replace("/;[\r\n]+/",";",$css_code);
  229.             $css_code=preg_replace("/[\r\n]+/","\n",$css_code);
  230.             $css_code=preg_replace("/\/\*.*\n/","",$css_code);
  231.             //$css_code=preg_replace("/\n/","\";str+=\"",$css_code);
  232.             //$css_code=preg_replace("/\n/","\\n",$css_code);
  233.         
  234.             $css_code=preg_replace("/\{ /","{",$css_code);
  235.             $css_code=preg_replace("/[; ]+\}/","}",$css_code);
  236.        }else{
  237.                file_put_contents($location."/temp.css",$css_code);
  238.             `java  -jar ./yui/yuicompressor-2.3.5/build/yuicompressor-2.3.5.jar {$location}/temp.css > {$location}/temp2.css`;
  239.             $css_code=file_get_contents($location."/temp2.css");
  240.             unlink($location."/temp.css");
  241.             unlink($location."/temp2.css");
  242.        }
  243.         
  244.     
  245.         file_put_contents($location."/dhtmlx.js",$js_header.$js_code);
  246.         file_put_contents($location."/dhtmlx.css",$css_code);
  247.         file_put_contents($location."/manifest.txt",implode("\n",$manifest));
  248.         
  249.         return $location;
  250. }
  251.  
  252. ?>