Commit dd40b47a authored by Filipe Ferreira's avatar Filipe Ferreira
Browse files

Issue #2789865: Code cleanup and review

parent 6f3520be
Loading
Loading
Loading
Loading
+238 −159
Original line number Diff line number Diff line
@@ -129,7 +129,10 @@ function netforum_test_page() {
  drupal_add_js("var xweb_functions = " . drupal_json_encode($xweb_functions) . ";", 'inline');
  //this is the later use referenced above.  The js here will adjust the form fields
  //and labels based on the selected function
  drupal_add_js(drupal_get_path('module', 'netforum') . '/jquery.netforum.js', array('type' => 'file', 'scope' => 'footer'));
  drupal_add_js(drupal_get_path('module', 'netforum') . '/jquery.netforum.js', array(
    'type' => 'file',
    'scope' => 'footer'
  ));
  drupal_add_js('misc/collapse.js', 'file');

  return drupal_get_form('netforum_test_form', $response, $request_code);
@@ -143,7 +146,7 @@ function netforum_test_page() {
 * @see netforum_test_page()
 */
function netforum_test_form($form, &$form_state, $response, $request_code = '') {
  $form_state['no_cache'] = true; # Do not cache this form, some of the vars may include unserializable data
  $form_state['no_cache'] = TRUE; # Do not cache this form, some of the vars may include unserializable data

  $request_options = netforum_xweb_functions();
  if (count($request_options) == 0) {
@@ -359,7 +362,10 @@ function netforum_objects_autocomplete($search = "") {
  drupal_add_http_header('Content-Type', 'text/javascript; charset=utf-8');
  $obj_names = array();
  if ($search != "") {
    $results = db_query("SELECT obj_name FROM {netforum_object_cache} WHERE obj_name like :obj_name OR obj_prefix like :obj_prefix ORDER BY obj_name", array(':obj_name' => "$search%", ':obj_prefix' => "$search%"));
    $results = db_query("SELECT obj_name FROM {netforum_object_cache} WHERE obj_name like :obj_name OR obj_prefix like :obj_prefix ORDER BY obj_name", array(
      ':obj_name' => "$search%",
      ':obj_prefix' => "$search%"
    ));
    foreach ($results as $obj) {
      $obj_names[$obj->obj_name] = check_plain($obj->obj_name);
    }
@@ -625,7 +631,10 @@ function netforum_is_available() {
  $verify_length = variable_get('netforum_verify_length', 3);
  if ($verify_length != 0) {
    $cutoff = strtotime("-" . $verify_length . " minutes");
    $last_result = variable_get('netforum_netforum_is_available', array(NULL, NULL));
    $last_result = variable_get('netforum_netforum_is_available', array(
      NULL,
      NULL
    ));
    if (is_null($last_result[0]) === FALSE && $last_result[1] >= $cutoff) {
      return $last_result[0];
    }
@@ -661,7 +670,10 @@ function netforum_is_available() {
  error_reporting($error_level);

  if ($verify_length != 0) {
    variable_set('netforum_netforum_is_available', array($_netforum_is_available, REQUEST_TIME));
    variable_set('netforum_netforum_is_available', array(
      $_netforum_is_available,
      REQUEST_TIME
    ));
  }

  return $_netforum_is_available;
@@ -812,10 +824,18 @@ function netforum_xweb_request($fname, $arguments = array(), $cache_max = NULL)
      }
      $nfh->setCaching(variable_get('netforum_cache_default', '4 hours'));
      if ($fname == 'GetQuery') {
        watchdog('netforum', "Could not get response to !fname request for !columns from !object: @fault", array('!fname' => $fname, '@fault' => $e->faultstring, '!columns' => $arguments['szColumnList'], '!object' => $arguments['szObjectName']), WATCHDOG_ERROR);
        watchdog('netforum', "Could not get response to !fname request for !columns from !object: @fault", array(
          '!fname' => $fname,
          '@fault' => $e->faultstring,
          '!columns' => $arguments['szColumnList'],
          '!object' => $arguments['szObjectName']
        ), WATCHDOG_ERROR);
      }
      else {
        watchdog('netforum', "Could not get response to !fname request: @fault", array('!fname' => $fname, '@fault' => $e->faultstring), WATCHDOG_ERROR);
        watchdog('netforum', "Could not get response to !fname request: @fault", array(
          '!fname' => $fname,
          '@fault' => $e->faultstring
        ), WATCHDOG_ERROR);
      }
    }
    else {
@@ -842,7 +862,12 @@ function netforum_xweb_request($fname, $arguments = array(), $cache_max = NULL)
    $elapsed_time = microtime() - $start_time;
    $max_time = (float) variable_get('netforum_slow_query_limit', '1.5');
    if ($max_time != 0 && $elapsed_time > $max_time) {
      watchdog('netforum', "Slow returning response to xWeb query !fname !source in !elapsed_time seconds, parameters: <br> @arguments", array('!fname' => $fname, '!source' => $source, '!elapsed_time' => $elapsed_time, '@arguments' => print_r($arguments, TRUE)), WATCHDOG_WARNING);
      watchdog('netforum', "Slow returning response to xWeb query !fname !source in !elapsed_time seconds, parameters: <br> @arguments", array(
        '!fname' => $fname,
        '!source' => $source,
        '!elapsed_time' => $elapsed_time,
        '@arguments' => print_r($arguments, TRUE)
      ), WATCHDOG_WARNING);
    }
  }

@@ -976,7 +1001,10 @@ function netforum_object_fields($obj_name) {


  $updated_object_data['data'] = serialize($obj_fields);
  db_update('netforum_object_cache')->fields($updated_object_data)->condition('obj_name', $obj_name)->execute();
  db_update('netforum_object_cache')
    ->fields($updated_object_data)
    ->condition('obj_name', $obj_name)
    ->execute();


  $seen_objects[$obj_name] = $obj_fields;
@@ -1124,7 +1152,10 @@ function netforum_refresh_object_names() {
    }

    foreach ($to_update as $obj_key => $obj) {
      db_update('netforum_object_cache')->fields($obj)->condition('obj_key', $obj_key)->execute();
      db_update('netforum_object_cache')
        ->fields($obj)
        ->condition('obj_key', $obj_key)
        ->execute();
    }
  }
}
@@ -1420,7 +1451,7 @@ function _netforum_xweb_soap_functions() {
      $function_name = trim(substr($function, $start, ($end - $start)));
      $function_display = trim(substr($function, $start));
      //Try to present and display functions that take discrete items, instead of custom structs where possible
      if (isset($xweb_functions[$function_name]) == false || (stristr($xweb_functions[$function_name], '$parameters') && stristr($function_display, '$'))) {
      if (isset($xweb_functions[$function_name]) == FALSE || (stristr($xweb_functions[$function_name], '$parameters') && stristr($function_display, '$'))) {
        $xweb_functions[$function_name] = $function_display;
      }
    }
@@ -1457,10 +1488,10 @@ function netforum_xweb_function_parameters($fname) {
 */
function netforum_xweb_function_struct_parameters($fname) {
  if (netforum_is_enterprise()) {
    return _netforum_xweb_soap_function_parameters($fname, true);
    return _netforum_xweb_soap_function_parameters($fname, TRUE);
  }
  else {
    return _netforum_xweb_od_function_parameters($fname, true);
    return _netforum_xweb_od_function_parameters($fname, TRUE);
  }
}

@@ -1478,7 +1509,7 @@ function netforum_xweb_function_struct_parameters($fname) {
 * @return
 *    An array of parameter names
 */
function _netforum_xweb_soap_function_parameters($fname, $inspect_structs = false) {
function _netforum_xweb_soap_function_parameters($fname, $inspect_structs = FALSE) {
  static $function_parameters = array();
  $parameter_names = array();

@@ -1533,7 +1564,7 @@ function _netforum_xweb_soap_function_parameters($fname, $inspect_structs = fals
 * @return
 *    An array of parameter names
 */
function _netforum_xweb_od_function_parameters($fname, $inspect_structs = false) {
function _netforum_xweb_od_function_parameters($fname, $inspect_structs = FALSE) {

  if (!isset($fname) || $fname == '') {
    return array();
@@ -1543,14 +1574,41 @@ function _netforum_xweb_od_function_parameters($fname, $inspect_structs = false)
  $functions['CheckEWebUser'] = array('szEmail', 'szPassword');
  $functions['GetCommitteeByCode'] = array('szCode');
  $functions['GetCommitteeByKey'] = array('szKey');
  $functions['GetCommitteeListByCode'] = array('szCode', 'szActiveInactiveType', 'szRecordDate');
  $functions['GetCommitteeListByCode'] = array(
    'szCode',
    'szActiveInactiveType',
    'szRecordDate'
  );
  $functions['GetCommitteeListByCstId'] = array('szCstId');
  $functions['GetCustomerBalance'] = array('szCstKey');
  $functions['GetCustomerByCityState'] = array('szState', 'szCity', 'bIncludeIndividuals', 'bIncludeOrganizations', 'szRecordDate');
  $functions['GetCustomerByCityState'] = array(
    'szState',
    'szCity',
    'bIncludeIndividuals',
    'bIncludeOrganizations',
    'szRecordDate'
  );
  $functions['GetCustomerById'] = array('szCstId');
  $functions['GetCustomerByName'] = array('szName');
  $functions['GetCustomerByRecordDate'] = array('szRecordDate', 'bMembersOnly', 'bIncludeOrganizations', 'bIncludeIndividuals');
  $functions['GetCustomerByZip'] = array('bIncludeIndividuals', 'bIncludeOrganizations', 'zip1', 'zip2', 'zip3', 'zip4', 'zip5', 'zip6', 'zip7', 'zip8', 'zip9');
  $functions['GetCustomerByRecordDate'] = array(
    'szRecordDate',
    'bMembersOnly',
    'bIncludeOrganizations',
    'bIncludeIndividuals'
  );
  $functions['GetCustomerByZip'] = array(
    'bIncludeIndividuals',
    'bIncludeOrganizations',
    'zip1',
    'zip2',
    'zip3',
    'zip4',
    'zip5',
    'zip6',
    'zip7',
    'zip8',
    'zip9'
  );
  $functions['GetCustomerEvent'] = array('szCstKey', 'szRecordDate');
  $functions['GetCustomerMembership'] = array('szCstKey');
  $functions['GetCustomerSession'] = array('szCstKey', 'szRecordDate');
@@ -1559,8 +1617,17 @@ function _netforum_xweb_od_function_parameters($fname, $inspect_structs = false)
  $functions['GetEventListByName'] = array('szName', 'szRecordDate');
  $functions['GetFacadeObject'] = array('szObjectName', 'szObjectKey');
  $functions['GetFacadeXMLSchema'] = array('szObjectName');
  $functions['GetProductBalances'] = array('szCstKey', 'szPrdKey', 'szRecordDate');
  $functions['GetQuery'] = array('szObjectName', 'szColumnList', 'szWhereClause', 'szOrderBy');
  $functions['GetProductBalances'] = array(
    'szCstKey',
    'szPrdKey',
    'szRecordDate'
  );
  $functions['GetQuery'] = array(
    'szObjectName',
    'szColumnList',
    'szWhereClause',
    'szOrderBy'
  );
  $functions['GetSessionByKey'] = array('szKey');
  $functions['GetSessionCustomerList'] = array('szSessionKey', 'szSessionKey');
  $functions['GetSessionListByEvent'] = array('szEventKey', 'szRecordDate');
@@ -1615,7 +1682,8 @@ function netforum_xweb_struct_params($ptype, $pname) {
  if (array_key_exists($ptype, $types) && is_array($types[$ptype])) { //If it is an array we have already parsed it
    return $types[$ptype];
  }
  else if (array_key_exists($ptype, $types) && is_string($types[$ptype])) { //If it is a string, we need to parse it
  else {
    if (array_key_exists($ptype, $types) && is_string($types[$ptype])) { //If it is a string, we need to parse it
      static $call_stack = array();
      if (in_array($ptype, $call_stack)) {
        $types[$ptype] = array(); // Some of the definitions are recursive, so we make sure not to go too deep
@@ -1662,6 +1730,7 @@ function netforum_xweb_struct_params($ptype, $pname) {
      return $pname;
    }
  }
}

/**
 * Turn an array into an XML snippet for xWeb individual information requests
@@ -1823,7 +1892,11 @@ function _netforum_guess_xweb_url($url) {
  $guess = $url;
  $xweb_dir = "/xWeb/Secure/netFORUMXML.asmx?WSDL";

  $od_servers = array('66.28.41.163', 'netforum.avectra.com', 'www.netforumondemand.com');
  $od_servers = array(
    '66.28.41.163',
    'netforum.avectra.com',
    'www.netforumondemand.com'
  );
  $od_cert_server = 'www.netforumondemand.com';

  $parts = parse_url($url);
@@ -1858,10 +1931,12 @@ function _netforum_guess_xweb_url($url) {
    if (strtolower(substr($guess, -4)) == 'xweb') {
      $guess = $guess . "/Secure/netFORUMXML.asmx?WSDL";
    }
    else if (strtolower(substr($guess, -6)) == 'secure') {
    else {
      if (strtolower(substr($guess, -6)) == 'secure') {
        $guess = $guess . "/netFORUMXML.asmx?WSDL";
      }
    }
  }

  //If they are not using xWeb secure
  if (stristr($guess, 'xweb/netFORUMXML.asmx')) {
@@ -1909,7 +1984,11 @@ Once you have !set_up xWeb visit the !xweb_testing page to view the request and
<p>
If you are having problems, be sure to check the !log_page to see more information.
</p>
', array('!log_page' => l(t('log page'), 'admin/reports/watchdog'), '!set_up' => l(t('set up'), 'admin/config/netforum/connection'), '!xweb_testing' => l(t('xWeb testing page'), 'admin/config/netforum/xwebtest')));
', array(
        '!log_page' => l(t('log page'), 'admin/reports/watchdog'),
        '!set_up' => l(t('set up'), 'admin/config/netforum/connection'),
        '!xweb_testing' => l(t('xWeb testing page'), 'admin/config/netforum/xwebtest')
      ));
      return $output;
  }
}
@@ -1944,7 +2023,7 @@ function netforum_aes_encrypt($plaintext, $key, $iv) {
    return base64_encode($cipherText);
  }
  else {
    return false;
    return FALSE;
  }
}

+68 −59
Original line number Diff line number Diff line
@@ -29,6 +29,7 @@
 xWebSecureClient class.

 */

class xwebSecureClient extends SoapClient {
  private $userName;
  private $userPass;
@@ -39,18 +40,24 @@ class xwebSecureClient extends SoapClient {

  //new for response caching
  private $cacheExpireTime = ""; //the default is 4 hours ago, and it is set in the constructor
  private $cachedFunctions = array('GetDynamicQuery', 'GetFacadeObject', 'GetIndividualInformation', 'GetOrganizationInformation', 'GetQuery');
  private $cachedFunctions = array(
    'GetDynamicQuery',
    'GetFacadeObject',
    'GetIndividualInformation',
    'GetOrganizationInformation',
    'GetQuery'
  );
  private $cacheWsdlLoc = "";
  private $cachingOn = false;
  private $cachedResponse = false;
  private $responseFromCache = false;
  private $cachingOn = FALSE;
  private $cachedResponse = FALSE;
  private $responseFromCache = FALSE;

  private $offlineMode = false;
  private $offlineMode = FALSE;

  //for debugging purposes.  Not perfect, but if something is going awry it gives some insight
  public $log;

  function __construct($wsdl, $options = null) {
  function __construct($wsdl, $options = NULL) {
    if (isset($options['xwebUserName'], $options['xwebUserPass'])) {
      $this->userName = $options['xwebUserName'];
      $this->userPass = $options['xwebUserPass'];
@@ -83,23 +90,25 @@ class xwebSecureClient extends SoapClient {
    //overload the soap call function to only take a wsdl function name and an array of arguments, inject our auth token, and save the response auth token
    $this->log .= "Beginning __SoapCall\n";
    $responseHeaders = '';
    $this->cachedResponse = false;
    $this->cachedResponse = FALSE;

    if ($this->cachingOn === true ) {
    if ($this->cachingOn === TRUE) {
      $response = $this->cacheRetreive($fname, $arguments);
      if ($response) {
        $this->log .= "Returning cached response to call\n";
        $this->cachedResponse = true;
        $this->cachedResponse = TRUE;
        return $response;
      }
      else if ($this->offlineMode === true) { // if we're in offline mode don't continue the request to the live server
      else {
        if ($this->offlineMode === TRUE) { // if we're in offline mode don't continue the request to the live server
          return;
        }
      }
    }
    try {
      $response = parent::__soapCall($fname, $arguments, null, $this->getAuthHeaders(), $responseHeaders);
      $response = parent::__soapCall($fname, $arguments, NULL, $this->getAuthHeaders(), $responseHeaders);
      $this->authToken = $responseHeaders['AuthorizationToken']->Token;
      if ($this->cachingOn === true && in_array($fname, $this->cachedFunctions)) {
      if ($this->cachingOn === TRUE && in_array($fname, $this->cachedFunctions)) {
        $this->log .= "Caching response to soap call for future use\n";
        $this->cacheStore($fname, $arguments, $response);
      }
@@ -110,11 +119,11 @@ class xwebSecureClient extends SoapClient {
        $this->log .= "Caught exception with invalid token value, re-authenticating and trying one more time\n";
        $this->authToken = '';
        try {
          $response = parent::__soapCall($fname, $arguments, null, $this->getAuthHeaders(), $responseHeaders);
          $response = parent::__soapCall($fname, $arguments, NULL, $this->getAuthHeaders(), $responseHeaders);
          $this->authToken = $responseHeaders['AuthorizationToken']->Token;
          //store the auth token with drupal for later use
          //variable_set('netforum_auth_token',$this->authToken);
          if ($this->cachingOn === true && in_array($fname, $this->cachedFunctions)) {
          if ($this->cachingOn === TRUE && in_array($fname, $this->cachedFunctions)) {
            $this->log .= "Caching response to soap call for future use\n";
            $this->cacheStore($fname, $arguments, $response);
          }
@@ -159,15 +168,15 @@ class xwebSecureClient extends SoapClient {
      }
      $this->cacheExpireTime = strftime("%Y-%m-%d %H:%M", $expire_time);
    }
    $this->cachingOn = true;
    return true;
    $this->cachingOn = TRUE;
    return TRUE;
  }

  //turns the caching off
  public function disableCaching() {
    if ($this->cachingOn) {
      $this->log .= "Disabling caching\n";
      $this->cachingOn = false;
      $this->cachingOn = FALSE;
    }
  }

@@ -188,14 +197,14 @@ class xwebSecureClient extends SoapClient {
  public function enableOfflineMode() {
    if ($this->cachingOn) {
      $this->log .= "Enabling offline mode\n";
      $this->offlineMode = true;
      $this->offlineMode = TRUE;
    }
  }

  public function disableOfflineMode() {
    if ($this->offlineMode === true) {
    if ($this->offlineMode === TRUE) {
      $this->log .= "Disabling offline mode\n";
      $this->offlineMode = false;
      $this->offlineMode = FALSE;
    }
  }

@@ -213,7 +222,7 @@ class xwebSecureClient extends SoapClient {
      $responseHeaders = '';
      try {
        //run the soap call to get it - with the headers.  Use the parent soap call in case we overload our soap method
        $response = parent::__SoapCall("Authenticate", array('parameters' => $authReqParams), null, null, $responseHeaders);
        $response = parent::__SoapCall("Authenticate", array('parameters' => $authReqParams), NULL, NULL, $responseHeaders);
        $this->authToken = $responseHeaders['AuthorizationToken']->Token;
        //store the auth token with drupal for later use
        //variable_set('netforum_auth_token',$this->authToken);
@@ -225,7 +234,7 @@ class xwebSecureClient extends SoapClient {
    }

    //return the header we oh so want.
    return new SoapHeader($this->xwebNamespace, 'AuthorizationToken', array('Token' => $this->authToken), true);
    return new SoapHeader($this->xwebNamespace, 'AuthorizationToken', array('Token' => $this->authToken), TRUE);
  }

  private function setOverloadedWsdlFunctions() {
@@ -245,11 +254,11 @@ class xwebSecureClient extends SoapClient {
  private function cacheStore($fname = '', $arguments = '', $response = '') {
    if ($fname == '' || $arguments == '' || (!is_object($response) && $response == '')) {
      $this->log .= "Could not store response, invalid parameters passed\n";
      return false;
      return FALSE;
    }
    if (!in_array($fname, $this->cachedFunctions)) {
      $this->log .= "Could not store response, $fnanme is not on the list of cacheable functions\n";
      return false;
      return FALSE;
    }

    try {
@@ -265,23 +274,23 @@ class xwebSecureClient extends SoapClient {
      )->execute();
    }
    catch (Exception $exception) {
      $this->log .= "Error on insert - " . print_r($exception->errorInfo, true). "\n";
      return false;
      $this->log .= "Error on insert - " . print_r($exception->errorInfo, TRUE) . "\n";
      return FALSE;
    }

    $this->log .= "Cached call to $fname\n";
    return true;
    return TRUE;

  }

  private function cacheRetreive($fname = '', $arguments = '') {
    if ($fname == '' || $arguments == '') {
      $this->log .= "Could not fetch response from cache, invalid parameters passed\n";
      return false;
      return FALSE;
    }
    if (!in_array($fname, $this->cachedFunctions)) {
      $this->log .= "Could not fetch response from cache, $fname is not on the list of cacheable functions\n";
      return false;
      return FALSE;
    }

    $res = db_query("SELECT response FROM {netforum_request_cache}
@@ -302,12 +311,12 @@ class xwebSecureClient extends SoapClient {

    if ($res && $data = $res->fetchObject()) {
      $this->log .= "Found cached response to $fname, returning from database\n";
      $this->responseFromCache = true;
      $this->responseFromCache = TRUE;
      return unserialize($data->response); //since fetch row returns an array, we just want the plain ol' response
    }
    else {
      $this->log .= "No cached response found for $fname in database\n";
      return false;
      return FALSE;
    }
  }

+59 −56

File changed.

Preview size limit exceeded, changes collapsed.

+65 −65

File changed.

Contains only whitespace changes.

+1 −1

File changed.

Contains only whitespace changes.