# This patch file was generated by NetBeans IDE
# This patch can be applied using context Tools: Apply Diff Patch action on respective folder.
# It uses platform neutral UTF-8 encoding.
# Above lines and this line are ignored by the patching process.
Index: contributions/modules/soapclient/modules/soapclient/soapclient.install
--- contributions/modules/soapclient/modules/soapclient/soapclient.install No Base Revision
+++ contributions/modules/soapclient/modules/soapclient/soapclient.install Locally New
@@ -0,0 +1,71 @@
+<?php
+// $Id$
+
+/**
+ * @file
+ * soapclient.install
+ *
+ * Upgrade paths and install/uninstall functions
+ */
+
+/**
+ * Implementation of hook_uninstall();
+ */
+function soapclient_uninstall() {
+
+  variable_del('soapclient_lib');
+  variable_del('soapclient_nusoap_path');
+  variable_del('soapclient_proxyhost');
+  variable_del('soapclient_proxyport');
+  variable_del('soapclient_proxyuser');
+  variable_del('soapclient_proxypass');
+  variable_del('soapclient_reqtimeout');
+  variable_del('soapclient_reptimeout');
+  variable_del('soapclient_wsdlcache_path');
+  variable_del('soapclient_wsdlcache_lifetime');
+
+}
+
+
+/**
+ * Implementation of hook_requirements();
+ */
+function soapclient_requirements($phase) {
+  $requirements = array();
+  $t = get_t();
+
+    // check for SOAP library
+    $requirements['soapclient'] = array(
+      'title' => $t('SOAP Client'),
+      'value' => $t('PHP5 SOAP or nuSOAP')
+    );
+
+    //Find if nuSOAP library is installed
+    $searchdir[] = './sites/all';
+    $searchdir[] = './'. conf_path();
+
+    //Get current list of nusoap libraries installed. Just looks for nusoap.php file
+    $nusoap_libs = array();
+    foreach ($searchdir as $dir) {
+      $nusoap_libs = array_merge($nusoap_libs, file_scan_directory($dir, "nusoap\.php$"));
+    }
+
+    if (!count($nusoap_libs) && !extension_loaded('soap')) {
+      $requirements['soapclient']['severity'] = REQUIREMENT_ERROR;
+      $requirements['soapclient']['value'] = $t('Neither nuSOAP nor PHP5 SOAP are available for this system');
+      $requirements['soapclient']['description'] = $t('Soapclient module needs nuSOAP or PHP5 SOAP extension available. Currently, any of these two libraries was found installed. Please, follow the instructions about installing nuSOAP or '.
+                                                      'enabling SOAP extension if you are using PHP5 in the documentation.');
+    }
+
+    if ( count($nusoap_libs) && !file_exists(variable_get('soapclient_nusoap_path', '')) ) {
+      $requirements['soapclient']['severity'] = REQUIREMENT_WARNING;
+      $requirements['soapclient']['value'] = $t('A nuSOAP library has been detected in the system, but is not configured to be used by the Soap Client module.');
+      $requirements['soapclient']['description'] = $t('Please, !configure the SoapClient module to use this library path:<br> !lib',
+                                                     array(
+                                                       '!configure' => l('configure', 'admin/settings/soapclient'),
+                                                       '!lib' => reset($nusoap_libs)->filename)
+                                                     );
+    }
+
+  return $requirements;
+}
Index: contributions/modules/soapclient/modules/soapclient/soapclient.module
--- contributions/modules/soapclient/modules/soapclient/soapclient.module Base (1.1.2.3)
+++ contributions/modules/soapclient/modules/soapclient/soapclient.module Locally Modified (Based On 1.1.2.3)
@@ -16,57 +16,12 @@
  * --------------------------------- */
 
 /**
- * Implementation of hook_requirements();
- */
-function soapclient_requirements($phase) {
-  $t = get_t();
-
-  // check for SOAP library
-  $requirements['soapclient'] = array(
-    'title' => $t('SOAP Client'),
-    'value' => $t('PHP5 SOAP or nuSOAP')
-  );
-
-  if (!file_exists(realpath(dirname(__FILE__) .'/nusoap/nusoap.php')) && !extension_loaded('soap')) {
-    $requirements['soapclient']['severity'] = REQUIREMENT_ERROR;
-    $requirements['soapclient']['value'] = $t('Neither nuSOAP nor PHP5 SOAP exist');
-    $requirements['soapclient']['description'] = $t('Reinstall full package of soapclient.module, or '.
-                                                    'enabling SOAP extension, if you are using PHP5');
-  }
-
-  return $requirements;
-}
-
-/**
  * Implementation of hook_init();
  */
 function soapclient_init() {
-  global $_soapclient_LIBRARY;
   global $_soapclient_MCRYPT;
   global $_soapclient_AES128;
 
-  switch ( variable_get('soapclient_lib', 'auto') ) {
-    case 'auto':
-      // check for PHP5SOAP first, the load
-      // the nuSOAP, if PHP5SOAP not available.
-
-    case 'PHP5SOAP':
-      if ( extension_loaded('soap') ) {
-        $_soapclient_LIBRARY = 'PHP5SOAP';
-        break;
-      }
-      // no PHP5SOAP, loading nuSOAP
-
-    case 'nuSOAP':
-      require_once('nusoap/nusoap.php');
-      require_once('nusoap/class.wsdlcache.php');
-      $_soapclient_LIBRARY = 'nuSOAP';
-      break;
-  }
-  
-  // load wrapper class
-  require_once('lib/drupal_soap_client.inc');
-  
   // check, which encryption library to be used.
   $_soapclient_MCRYPT = NULL;
   $_soapclient_AES128 = NULL;
@@ -169,10 +124,33 @@
   return $output;
 }
 
+/**
+ * Traverse the sites/all and sites/%current_site directories looking for any
+ * installed version of the nuSOAP library.
+ *
+ * @return
+ * Array of nusoap libraries found in the system
+ */
+function soapclient_find_nusoap_library() {
+  //find installed nuSOAP libraries in sites/%current_site and sites/all. Give
+  //prio to sites/all/* files, then current site directory, then default. See
+  //includes/boostrap.inc for conf_path()
+  $searchdir[] = './sites/all';
+  $searchdir[] = './'. conf_path();
+
+  //Get current list of nusoap libraries installed. Just looks for nusoap.php file
+  $nusoap_libs = array();
+  foreach ($searchdir as $dir) {
+    $nusoap_libs = array_merge($nusoap_libs, file_scan_directory($dir, "nusoap\.php$"));
+  }
+  return $nusoap_libs;
+}
+    
 // configuration form
 function soapclient_config() {
-  global $_soapclient_LIBRARY;
 
+  $_soapclient_LIBRARY = variable_get('soapclient_lib', 'auto');
+
   $form    = array();
   $libinfo = soapclient_get_libname();
 
@@ -203,10 +181,35 @@
                           array('!lib' => t($libinfo['desc']) )),
   );
 
+  //Find all the possible installed version of nuSOAP library
+  $nusoap_libs = soapclient_find_nusoap_library();
+
+  //@TODO: If phpsoap is not enabled, and auto or nusoap have been selected, and no nusoap is detected, drop a message
+  //Addecuate a message for the admin to configure nuSOAP library.
+  if (count($nusoap_libs)) {
+    foreach ($nusoap_libs as $lib) {
+      $locations .= $lib->filename ."\n";
+    }
+  }
+
+  //get the first occurrence of nusoap.php from the scan results
+  $default_library = (is_array($nusoap_libs) && count($nusoap_libs)) ? reset($nusoap_libs)->filename : NULL;
+
+  $form['soapclient_nusoap_path'] = array(
+    '#type'    => 'textfield',
+    '#title'   => t('nuSOAP location'),
+    '#size'  => 120,
+    '#maxlength' => 255,
+    '#default_value' => variable_get('soapclient_nusoap_path', $default_library),
+    '#description'   => t('The location of the nusoap library. '.
+                          'This is the result of the nuSOAP library SCAN:<strong><br><pre>!lib</pre></strong>',
+                          array('!lib' => !empty($locations) ? $locations : "nuSOAP library Not found."  )),
+  );
+
   $form['soapclient_proxyhost'] = array(
     '#type'  => 'textfield',
     '#title' => t('Proxy Host'),
-    '#size'  => 120,
+    '#size'  => 32,
     '#maxlength'     => 256,
     '#default_value' => variable_get('soapclient_proxyhost', ''),
     '#description'   => t('Enter the IP address or host name of the proxy server between '.
@@ -272,7 +275,7 @@
       '#size'  => 64,
       '#maxlength'     => 255,
       '#default_value' => variable_get('soapclient_wsdlcache_path', variable_get('file_directory_temp', NULL)),
-      '#description'   => t('Enter the directory to allow nuSOAP keep it\'s WSDL cache. Note that the '.
+      '#description'   => t('Enter the directory to allow soap client keep it\'s WSDL cache. Note that the '.
                             'read/write permission is required.'),
     );
 
@@ -291,7 +294,7 @@
 
 // SOAP testing form
 function soapclient_test() {
-  global $_soapclient_LIBRARY;
+  $_soapclient_LIBRARY = variable_get('soapclient_lib', 'auto');
   
   $libinfo = soapclient_get_libname();
 
@@ -428,7 +431,20 @@
  *   - 'desc' : Full name of the library/extension.
  */
 function soapclient_get_libname() {
-  global $_soapclient_LIBRARY;
+  $_soapclient_LIBRARY = variable_get('soapclient_lib', 'auto');
+
+  //Convert 'auto' detection library into a real library
+  if (variable_get('soapclient_lib', 'auto') == 'auto') {
+    //check for PHP5SOAP first, use nuSOAP if PHP5SOAP not available.
+    if ( extension_loaded('soap') ) {
+      $_soapclient_LIBRARY = 'PHP5SOAP';
+    }
+    else {
+      if (file_exists(variable_get('soapclient_nusoap_path', ''))){
+        $_soapclient_LIBRARY = 'nuSOAP';
+      }
+    }
+  }
   $info = array();
   
   switch ($_soapclient_LIBRARY) {
@@ -444,7 +460,7 @@
 
     default:
       $info['lib']  = 'N/A';
-      $info['desc'] = 'None or not supported SOAP Client Libarary';
+      $info['desc'] = 'None or not supported SOAP Client Library';
       break;
   }
 
@@ -533,8 +549,21 @@
  *               for more details.
  */
 function soapclient_init_client($endpoint, $use_wsdl, $options = array()) {
-  global $_soapclient_LIBRARY;
 
+  $_soapclient_LIBRARY = variable_get('soapclient_lib', 'auto');
+  //Convert 'auto' detection library into a real library
+  if (variable_get('soapclient_lib', 'auto') == 'auto') {
+    //check for PHP5SOAP first, use nuSOAP if PHP5SOAP not available.
+    if ( extension_loaded('soap') ) {
+      $_soapclient_LIBRARY = 'PHP5SOAP';
+    }
+    else {
+      if (file_exists(variable_get(variable_get('soapclient_nusoap_path', '')))) {
+        $_soapclient_LIBRARY = 'nuSOAP';
+      }
+    }
+  }
+
   $client = NULL;
   $result = array();
   $result['#error']  = FALSE;  
@@ -568,7 +597,26 @@
   if (soapclient_empty_string($proxypass)) $proxypass = NULL;
 
   if ( $_soapclient_LIBRARY == 'nuSOAP' ) {
-    // prepare parameters for initialisation
+
+    //Checks for correct nuSOAP configuration.
+    //Check if library is configured.
+    $nusoap_location = variable_get('soapclient_nusoap_path', '');
+    if ( soapclient_empty_string($nusoap_location) ) {
+      $result['#error'] = t('nuSOAP library is not configured.');
+      return $result;
+    }
+
+    //Check if library is really there
+    if ( !file_exists($nusoap_location) ) {
+      $result['#error'] = t('nuSOAP library is not available at !lib!', array('!lib' => $_soapclient_LIBRARY) );
+      return $result;
+    }
+
+    //Load required library and cache classes.
+    require_once($nusoap_location);
+    require_once(str_replace('nusoap.php', 'class.wdslcache.php', $nusoap_location));
+
+    //prepare parameters for initialisation
     $reqtimeout = variable_get('soapclient_reqtimeout', 10);
     $reptimeout = variable_get('soapclient_reptimeout', 30);
 
@@ -596,6 +644,13 @@
     }
   }
   elseif ( $_soapclient_LIBRARY == 'PHP5SOAP' ) {
+
+    //Check PHP5SOAP module availability
+    if ( !extension_loaded('soap') ) {
+      $result['#error'] = t('PHP5 SOAP was the selected library to use by Soap Client module, but the extension is not enabled. Please follow the documentation of how to enable PHP5 SOAP extension.');
+      return $result;
+    }
+
\ No newline at end of file
     if ( ! $use_wsdl ) {
       if ( empty($options['namespace']) ) {
         $result['#error'] = t('Target namespace uri is required when using non-WSDL mode of PHP5 SOAP');
