--- nicedit.inc	2008-11-02 17:26:51.000000000 +0100
+++ nicedit_edit.inc	2009-02-08 21:13:46.000000000 +0100
@@ -1,7 +1,6 @@
 <?php
 // $Id: nicedit.inc,v 1.3 2008/11/02 16:26:51 sun Exp $
 
-
 /**
  * Plugin implementation of hook_editor().
  */
@@ -21,6 +20,8 @@ function wysiwyg_nicedit_editor() {
     'version callback' => 'wysiwyg_nicedit_version',
     'settings callback' => 'wysiwyg_nicedit_settings',
     'plugin callback' => 'wysiwyg_nicedit_plugins',
+    'plugin config callback' => 'wysiwyg_nicedit_plugins_config',
+    'upload callback' => 'wysiwyg_nicedit_upload',
     'versions' => array(
       '0.9' => array(
         'js files' => array('nicedit.js'),
@@ -89,6 +90,10 @@ function wysiwyg_nicedit_settings($edito
     }
   }
 
+  if (in_array('upload', $settings['buttonList']) && variable_get('clean_url', 0)) {
+    $settings['uploadURI'] = url("wysiwyg_upload/nicedit/$theme");
+  }
+
   return $settings;
 }
 
@@ -111,6 +116,7 @@ function wysiwyg_nicedit_plugins($editor
         'hr' => t('Horizontal rule'),
         // @todo New challenge: Optional internal plugins packaged into editor
         //   library.
+    		'upload' => 'Upload Image',
         'link' => t('Link'), 'unlink' => t('Unlink'),
         'fontFormat' => t('HTML block format'), 'fontFamily' => t('Font'), 'fontSize' => t('Font size'),
         'xhtml' => t('Source code'),
@@ -120,3 +126,67 @@ function wysiwyg_nicedit_plugins($editor
   );
 }
 
+/**
+ * Returns plugin specific form elements to the editor's configuration form.
+ * This is a callback defined in hook_editor().
+ *
+ * @see wysiwyg_profile_form(), wysiwyg_nicedit_editor()
+ */
+function wysiwyg_nicedit_plugins_config(&$form, $profile) {
+  if ($profile->settings['buttons']['default']['upload']) {
+    $form['uploadURI'] = array(
+      '#type' => 'textfield',
+      '#title' => t('Upload folder'),
+      '#default_value' => ($profile->settings['plugin_settings']['uploadURI'] ? $profile->settings['plugin_settings']['uploadURI'] : file_directory_path()),
+      '#description' => t('Specify in which folder the nicUpload plugin should move uploaded images.'),
+    );
+  }
+}
+
+/**
+ * Used to define upload validation settings for this editor. 
+ * This is a callback defined in hook_editor().
+ *
+ * @see wysiwyg_upload(), wysiwyg_nicedit_editor()
+ */
+function wysiwyg_nicedit_upload($theme, $format) {
+  $profile = wysiwyg_get_profile(str_replace("format", "", $format));
+
+  $id = $_GET['id'];
+  $upl_progress = $_POST['APC_UPLOAD_PROGRESS'];
+
+  if (isset($id, $upl_progress) && ($id == $upl_progress)) {
+    $_FILES['files']['name']['nicImage'] = $_FILES['nicImage']['name'];
+    $_FILES['files']['type']['nicImage'] = $_FILES['nicImage']['type'];
+    $_FILES['files']['tmp_name']['nicImage'] = $_FILES['nicImage']['tmp_name'];
+    $_FILES['files']['error']['nicImage'] = $_FILES['nicImage']['error'];
+    $_FILES['files']['size']['nicImage'] = $_FILES['nicImage']['size'];
+
+   $validators = array(
+      'file_validate_is_image' => array(),
+      'file_validate_image_resolution' => array('600x400'),
+      'file_validate_size' => array(variable_get('user_picture_file_size', '30') * 1024),
+    );
+    
+    $file = file_save_upload('nicImage', $validators, $profile->settings['plugin_settings']['uploadURI']);
+
+    $img_info = image_get_info($file->filepath);
+    $status['done'] = 1;
+    $status['width'] = $img_info['width'];
+    $status['url'] = base_path() . $file->filepath;
+
+    $script = '
+        try {
+            '.(($_SERVER['REQUEST_METHOD']=='POST') ? 'top.' : '').'nicUploadButton.statusCb('.json_encode($status).');
+        } catch(e) { alert(e.message); }
+    ';
+    
+    if($_SERVER['REQUEST_METHOD']=='POST') {
+        echo '<script>'.$script.'</script>';
+    } else {
+        echo $script;
+    }
+  } 
+
+  print drupal_to_js($r);
+}
\ No newline at end of file
