Index: jsmath.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/jsmath/jsmath.module,v
retrieving revision 1.7
diff -u -r1.7 jsmath.module
--- jsmath.module	11 Jul 2008 12:20:26 -0000	1.7
+++ jsmath.module	14 Jul 2008 14:56:46 -0000
@@ -6,6 +6,34 @@
  */
 function _jsmath_settings() {
 
+  $form['#submit'][] = 'jsmath_install_form_submit';
+  
+  $form['#attributes'] = array('enctype' => 'multipart/form-data');
+
+  $form['jsmath_install'] = array(
+    '#type' => 'fieldset', 
+    '#title' => t('Install jsMath'),
+  );
+  
+  $form['jsmath_install']['jsmath_file'] = array(
+    '#type' => 'file',
+    '#title' => t('Upload jsMath zip files'),
+    '#default_value' => '',
+    '#description' => t('This allows the "jsMath" zip file and the "jsMath fonts" zip file 
+      to be easily uploaded to the Drupal "files" folder. Simply upload each zip file
+      in turn and they will be automatically extracted to the correct place.<br />
+      WARNING: The "jsMath fonts" zip file may take a long time to extract.<br />
+      This feature requires PHP 5.2.0 or later and the php_zip extension enabled.
+      If your system does not meet these requirements, the zip files must be manually extracted 
+      to the Drupal "files" folder.'
+    )
+  );
+  
+  $form['jsmath_install']['submit'] = array(
+    '#type' => 'submit',
+    '#value' => t('Upload')
+  );
+  
   $form['jsmath_scale'] = array(
     '#type' => 'textfield',
     '#title' => t('scale'),
@@ -246,7 +274,7 @@
  */
 function jsmath_init() {
   // Create javascript for setting jsMath options and loading jsMath
-  $jsmath_path = base_path() . drupal_get_path('module', 'jsmath') . '/jsMath';
+  $jsmath_path = base_path() .'files/jsMath';
   $load = "<script type=\"text/javascript\">
     if (!window.jsMath) {window.jsMath = {}}
     
@@ -298,3 +326,144 @@
 
   return $items;
 }
+
+
+/**
+ * Handler for uploading zip files
+ */
+function jsmath_install_form_submit(&$form, &$form_state) {
+  if ($form_state['values']['op'] == t('Upload')) {
+  
+    if(isset($_FILES['files']['tmp_name']['jsmath_file']))
+    {
+      $tmp_file = $_FILES['files']['tmp_name']['jsmath_file'];
+      
+      if(jsmath_unzip($tmp_file, $_SERVER['DOCUMENT_ROOT'] . base_path() .'files/'))
+      {
+        drupal_set_message(t('Zip file extracted.'));
+      }
+      else
+      {
+        drupal_set_message(t('Error extracting zip file.'));
+      }
+    }
+  }
+}
+
+/**
+ * The following functions are taken from the PHP Zip Functions documentation
+ * http://uk2.php.net/manual/en/ref.zip.php 
+ * Posted by darkstream777 at gmx dot net, 15-May-2007 12:19
+ * (slightly modified to prefix the function names with jsmath_ and to use drupal_set_message()) 
+ */
+ 
+/**
+ * Unzip the source_file in the destination dir
+ *
+ * @param   string      The path to the ZIP-file.
+ * @param   string      The path where the zipfile should be unpacked, if false the directory of the zip-file is used
+ * @param   boolean     Indicates if the files will be unpacked in a directory with the name of the zip-file (true) or not (false) (only if the destination directory is set to false!)
+ * @param   boolean     Overwrite existing files (true) or not (false)
+ *
+ * @return  boolean     Succesful or not
+ */
+function jsmath_unzip($src_file, $dest_dir=false, $create_zip_name_dir=true, $overwrite=true)
+{
+  if(function_exists("zip_open"))
+  {  
+      if(!is_resource(zip_open($src_file)))
+      {
+          $src_file=dirname($_SERVER['SCRIPT_FILENAME'])."/".$src_file;
+      }
+     
+      if (is_resource($zip = zip_open($src_file)))
+      {         
+          $splitter = ($create_zip_name_dir === true) ? "." : "/";
+          if ($dest_dir === false) $dest_dir = substr($src_file, 0, strrpos($src_file, $splitter))."/";
+        
+          // Create the directories to the destination dir if they don't already exist
+          jsmath_create_dirs($dest_dir);
+
+          // For every file in the zip-packet
+          while ($zip_entry = zip_read($zip))
+          {
+            // Now we're going to create the directories in the destination directories
+          
+            // If the file is not in the root dir
+            $pos_last_slash = strrpos(zip_entry_name($zip_entry), "/");
+            if ($pos_last_slash !== false)
+            {
+              // Create the directory where the zip-entry should be saved (with a "/" at the end)
+              jsmath_create_dirs($dest_dir.substr(zip_entry_name($zip_entry), 0, $pos_last_slash+1));
+            }
+
+            // Open the entry
+            if (zip_entry_open($zip,$zip_entry,"r"))
+            {
+            
+              // The name of the file to save on the disk
+              $file_name = $dest_dir.zip_entry_name($zip_entry);
+            
+              // Check if the files should be overwritten or not
+              if ($overwrite === true || $overwrite === false && !is_file($file_name))
+              {
+                // Get the content of the zip entry
+                $fstream = zip_entry_read($zip_entry, zip_entry_filesize($zip_entry));          
+               
+                if(!is_dir($file_name))           
+                file_put_contents($file_name, $fstream );
+                // Set the rights
+                if(file_exists($file_name))
+                {
+                    chmod($file_name, 0777);
+                    //echo "<span style=\"color:#1da319;\">file saved: </span>".$file_name."<br />";
+                }
+                else
+                {
+                    //echo "<span style=\"color:red;\">file not found: </span>".$file_name."<br />";
+                }
+              }
+            
+              // Close the entry
+              zip_entry_close($zip_entry);
+            }     
+          }
+          // Close the zip-file
+          zip_close($zip);
+      }
+      else
+      {
+        //echo "No Zip Archive Found.";
+        return false;
+      }
+    
+      return true;
+  }
+  else
+  {
+      if(version_compare(phpversion(), "5.2.0", "<"))
+      $infoVersion="(use PHP 5.2.0 or later)";
+     
+      drupal_set_message(t("You need to install/enable the php_zip.dll extension $infoVersion"));
+  }
+}
+
+function jsmath_create_dirs($path)
+{
+  if (!is_dir($path))
+  {
+    $directory_path = "";
+    $directories = explode("/",$path);
+    array_pop($directories);
+  
+    foreach($directories as $directory)
+    {
+      $directory_path .= $directory."/";
+      if (!is_dir($directory_path))
+      {
+        mkdir($directory_path);
+        chmod($directory_path, 0777);
+      }
+    }
+  }
+}

