<commit_before><?php
sfContext::getInstance()->getConfiguration()->loadHelpers('Asset');

class tuiWidgetS3SWFUpload extends sfWidgetForm
{
  
  public function getJavascripts()
  {
    return array(
      'http://ajax.googleapis.com/ajax/libs/jquery/1.6/jquery.min.js',
      image_path('/tuiS3SWFUploadPlugin/js/swfupload.js'),
      'http://cachedcommons.org/cache/jquery-swfupload/1.0.0/javascripts/jquery-swfupload-min.js',
    );
  }
  
  
  public function configure($options = array(), $attributes = array())
  {
    $this->addRequiredOption('aws_accesskey');
    $this->addRequiredOption('aws_secret');
    $this->addRequiredOption('aws_bucket');
    
    $this->addOption('acl', 'private');
    $this->addOption('key', 'uploads/${filename}');
    $this->addOption('button_image', image_path('/tuiS3SWFUploadPlugin/images/XPButtonUploadText_61x22.png'));
    $this->addOption('size_limit', '2 GB');
    $this->addOption('file_types', '*.*');
    $this->addOption('file_types_description', 'All files');
    $this->addOption('button_width', 61);
    $this->addOption('button_height', 22);
    $this->addOption('debug', false);
    $this->addOption('upload_limit', 1);
    $this->addOption('queue_limit', 1);
    $this->addOption('rrs', false);


    $this->addOption('settings.js', '
      {
          // Backend Settings
          upload_url: {bucket_url},  
          http_success : [ 200, 201, 204 ],

          // File Upload Settings
          file_size_limit : {size_limit},
          file_types : {file_types},
          file_types_description : {file_types_description},
          file_upload_limit : {upload_limit},
          file_queue_limit : {queue_limit},
          file_post_name : "file",

          // Button settings
          button_image_url : {button_image},
          button_placeholder_id : {button_id},
          button_width: {button_width},
          button_height: {button_height},

          // Flash Settings
          flash_url: "'.image_path('/tuiS3SWFUploadPlugin/swf/swfupload.swf').'",
          debug: {debug},
          post_params: {
            "AWSAccessKeyId": {aws_accesskey},
            "acl": {acl},
            "key": {key},
            "policy": {policy_encoded},
            "signature": {policy_signature},
            "success_action_status" : "201"
            "x-amz-storage-class" : {rrs}
          }

      }
    ');


    
    $this->addOption('events.js', '
      .bind("fileQueued", function(event, file){
        $(this).swfupload("startUpload");
      })
      .bind("fileQueueError", function(event, file, errorCode, message){
        alert("File queue error:\n"+message);
      })
      .bind("uploadProgress", function(event, file, bytesLoaded){
        $(".progress", this).text((Math.floor((bytesLoaded / file.size) * 1000) / 10 )+ "%");
      })
      .bind("uploadComplete", function(event, file){
        $(".progress", this).text("Upload complete");

        // Change this callback function to suit your needs
         // $.ajax({
         //   type: "POST",
         //   url: "/upload.php",
         //   data: "name=" + file.name,
         //   async: false,
         // });

        // upload has completed, lets try the next one in the queue
        $(this).swfupload("startUpload");
      });
    ');
    
    $this->addOption('script.js', '
      <script type="text/javascript">
      $(function(){
          $("#{widget_id}").swfupload({settings.js}) 
          {events.js}
      });
      </script>
    ');
    
    $this->addOption('template.html', '
      <div id="{widget_id}" class="swfupload-control">
        <div id="{widget_id}-button">Upload</div>
        <div class="progress"></div>
      </div>
      {script.js}
    ');
  }
  
  
  
  public function render($name, $value = null, $attributes = array(), $errors = array())
  {
    
    $id = $this->generateId($name);
    $template_vars = array(
      '{widget_id}'              => $id,
      '{policy_signature}'       => json_encode($this->generateSignature()),
      '{policy_encoded}'         => json_encode(base64_encode(json_encode($this->getPolicy()))),
      '{key}'                    => json_encode($this->getOption('key')),
      '{acl}'                    => json_encode($this->getOption('acl')),
      '{aws_accesskey}'          => json_encode($this->getOption('aws_accesskey')),
      '{debug}'                  => json_encode($this->getOption('debug')),
      '{button_id}'              => json_encode($id.'-button'),
      '{button_image}'           => json_encode(image_path($this->getOption('button_image'))),
      '{button_width}'           => json_encode($this->getOption('button_width')),
      '{button_height}'          => json_encode($this->getOption('button_height')),
      '{queue_limit}'            => json_encode($this->getOption('queue_limit')),
      '{upload_limit}'           => json_encode($this->getOption('upload_limit')),
      '{file_types_description}' => json_encode($this->getOption('file_types_description')),
      '{file_types}'             => json_encode($this->getOption('file_types')),
      '{size_limit}'             => json_encode($this->getOption('size_limit')),
      '{bucket_url}'             => json_encode('http://'.$this->getOption('aws_bucket').'.s3.amazonaws.com/'),
      '{rrs}'                    => json_encode($this->getOption('rrs') ? 'REDUCED_REDUNDANCY' : 'STANDARD');
    );

    // Swap in the template contents and return the resulting HTML
    $template_vars['{settings.js}'] = strtr($this->getOption('settings.js'), $template_vars);
    $template_vars['{events.js}'] = strtr($this->getOption('events.js'), $template_vars);
    $template_vars['{script.js}']   = strtr($this->getOption('script.js'), $template_vars);
    return strtr($this->getOption('template.html'), $template_vars);
  }
  
  
  public function generateSignature()
  {<commit_msg>Fix syntax error<commit_after><?php
sfContext::getInstance()->getConfiguration()->loadHelpers('Asset');

class tuiWidgetS3SWFUpload extends sfWidgetForm
{
  
  public function getJavascripts()
  {
    return array(
      'http://ajax.googleapis.com/ajax/libs/jquery/1.6/jquery.min.js',
      image_path('/tuiS3SWFUploadPlugin/js/swfupload.js'),
      'http://cachedcommons.org/cache/jquery-swfupload/1.0.0/javascripts/jquery-swfupload-min.js',
    );
  }
  
  
  public function configure($options = array(), $attributes = array())
  {
    $this->addRequiredOption('aws_accesskey');
    $this->addRequiredOption('aws_secret');
    $this->addRequiredOption('aws_bucket');
    
    $this->addOption('acl', 'private');
    $this->addOption('key', 'uploads/${filename}');
    $this->addOption('button_image', image_path('/tuiS3SWFUploadPlugin/images/XPButtonUploadText_61x22.png'));
    $this->addOption('size_limit', '2 GB');
    $this->addOption('file_types', '*.*');
    $this->addOption('file_types_description', 'All files');
    $this->addOption('button_width', 61);
    $this->addOption('button_height', 22);
    $this->addOption('debug', false);
    $this->addOption('upload_limit', 1);
    $this->addOption('queue_limit', 1);
    $this->addOption('rrs', false);


    $this->addOption('settings.js', '
      {
          // Backend Settings
          upload_url: {bucket_url},  
          http_success : [ 200, 201, 204 ],

          // File Upload Settings
          file_size_limit : {size_limit},
          file_types : {file_types},
          file_types_description : {file_types_description},
          file_upload_limit : {upload_limit},
          file_queue_limit : {queue_limit},
          file_post_name : "file",

          // Button settings
          button_image_url : {button_image},
          button_placeholder_id : {button_id},
          button_width: {button_width},
          button_height: {button_height},

          // Flash Settings
          flash_url: "'.image_path('/tuiS3SWFUploadPlugin/swf/swfupload.swf').'",
          debug: {debug},
          post_params: {
            "AWSAccessKeyId": {aws_accesskey},
            "acl": {acl},
            "key": {key},
            "policy": {policy_encoded},
            "signature": {policy_signature},
            "success_action_status" : "201",
            "x-amz-storage-class" : {rrs}
          }

      }
    ');


    
    $this->addOption('events.js', '
      .bind("fileQueued", function(event, file){
        $(this).swfupload("startUpload");
      })
      .bind("fileQueueError", function(event, file, errorCode, message){
        alert("File queue error:\n"+message);
      })
      .bind("uploadProgress", function(event, file, bytesLoaded){
        $(".progress", this).text((Math.floor((bytesLoaded / file.size) * 1000) / 10 )+ "%");
      })
      .bind("uploadComplete", function(event, file){
        $(".progress", this).text("Upload complete");

        // Change this callback function to suit your needs
         // $.ajax({
         //   type: "POST",
         //   url: "/upload.php",
         //   data: "name=" + file.name,
         //   async: false,
         // });

        // upload has completed, lets try the next one in the queue
        $(this).swfupload("startUpload");
      });
    ');
    
    $this->addOption('script.js', '
      <script type="text/javascript">
      $(function(){
          $("#{widget_id}").swfupload({settings.js}) 
          {events.js}
      });
      </script>
    ');
    
    $this->addOption('template.html', '
      <div id="{widget_id}" class="swfupload-control">
        <div id="{widget_id}-button">Upload</div>
        <div class="progress"></div>
      </div>
      {script.js}
    ');
  }
  
  
  
  public function render($name, $value = null, $attributes = array(), $errors = array())
  {
    
    $id = $this->generateId($name);
    $template_vars = array(
      '{widget_id}'              => $id,
      '{policy_signature}'       => json_encode($this->generateSignature()),
      '{policy_encoded}'         => json_encode(base64_encode(json_encode($this->getPolicy()))),
      '{key}'                    => json_encode($this->getOption('key')),
      '{acl}'                    => json_encode($this->getOption('acl')),
      '{aws_accesskey}'          => json_encode($this->getOption('aws_accesskey')),
      '{debug}'                  => json_encode($this->getOption('debug')),
      '{button_id}'              => json_encode($id.'-button'),
      '{button_image}'           => json_encode(image_path($this->getOption('button_image'))),
      '{button_width}'           => json_encode($this->getOption('button_width')),
      '{button_height}'          => json_encode($this->getOption('button_height')),
      '{queue_limit}'            => json_encode($this->getOption('queue_limit')),
      '{upload_limit}'           => json_encode($this->getOption('upload_limit')),
      '{file_types_description}' => json_encode($this->getOption('file_types_description')),
      '{file_types}'             => json_encode($this->getOption('file_types')),
      '{size_limit}'             => json_encode($this->getOption('size_limit')),
      '{bucket_url}'             => json_encode('http://'.$this->getOption('aws_bucket').'.s3.amazonaws.com/'),
      '{rrs}'                    => json_encode($this->getOption('rrs') ? 'REDUCED_REDUNDANCY' : 'STANDARD'),
    );

    // Swap in the template contents and return the resulting HTML
    $template_vars['{settings.js}'] = strtr($this->getOption('settings.js'), $template_vars);
    $template_vars['{events.js}'] = strtr($this->getOption('events.js'), $template_vars);
    $template_vars['{script.js}']   = strtr($this->getOption('script.js'), $template_vars);
    return strtr($this->getOption('template.html'), $template_vars);
  }
  
  
  public function generateSignature()
  {
{'fraction_non_alphanumeric': 0.18004430811179278, 'fraction_numerical': 0.006987048398091343, 'mean_word_length': 2.657525708943596, 'pattern_counts': {'":': 10, '<': 21, '<?xml version=': 0, '>': 139, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5812040', 'n_tokens_mistral': 3625, 'n_tokens_neox': 3564, 'n_words': 510}