/**
  * Embed all the external resources of the current output buffer inside a single file and outputs it.
  *
  * @param string $path Path to save the files to
  * 
  * @return void|string
  */
 private static function allInOne($path = null, $options = array())
 {
     if ($path) {
         self::$_path = rtrim($path, "/\\") . "/";
     }
     CApp::setMemoryLimit("256M");
     self::$_fp_out = CMbPath::getTempFile();
     $re_img = "/<img([^>]*)src\\s*=\\s*[\"']([^\"']+)[\"']([^>]*)(>|\$)/i";
     $re_link = "/<link[^>]*rel=\"stylesheet\"[^>]*href\\s*=\\s*[\"']([^\"']+)[\"'][^>]*>/i";
     $re_script = "/<script[^>]*src\\s*=\\s*[\"']([^\"']+)[\"'][^>]*>\\s*<\\/script>/i";
     $re_a = "/<a([^>]*)href\\s*=\\s*[\"']embed:([^\"']+)[\"']([^>]*)>/i";
     $ignore_scripts = !empty($options["ignore_scripts"]);
     // End Output Buffering
     ob_end_clean();
     ob_start();
     rewind(self::$_fp_in);
     while (!feof(self::$_fp_in)) {
         $line = fgets(self::$_fp_in);
         $line = preg_replace_callback($re_img, array('self', 'replaceImgSrc'), $line);
         $line = preg_replace_callback($re_link, array('self', 'replaceStylesheet'), $line);
         if (!$ignore_scripts) {
             $line = preg_replace_callback($re_script, array('self', 'replaceScriptSrc'), $line);
         }
         if (self::$_path) {
             $line = preg_replace_callback($re_a, array('self', 'replaceAEmbed'), $line);
         }
         fwrite(self::$_fp_out, $line);
     }
     ob_end_clean();
     $length = 0;
     rewind(self::$_fp_out);
     $full_str = "";
     while (!feof(self::$_fp_out)) {
         $line = fgets(self::$_fp_out);
         $length += strlen($line);
         $line = str_replace("[[AIO-length]]", CMbString::toDecaBinary($length), $line);
         if (strpos($line, "[[AIO-memory]]") !== false) {
             $line = str_replace("[[AIO-memory]]", self::getOutputMemory(true), $line);
         }
         if ($path) {
             $full_str .= $line;
         } else {
             echo $line;
         }
     }
     return $full_str;
 }