Skip to content

Commit

Permalink
Merge pull request thybag#51 from nesl247/fix_sponlineauth_soap_error
Browse files Browse the repository at this point in the history
SoapFault expects 2 parameters, this wasn't always given. Added curl_err...
  • Loading branch information
thybag committed Jan 24, 2014
2 parents 7462bce + adbb28c commit d4bfd3f
Showing 1 changed file with 54 additions and 54 deletions.
108 changes: 54 additions & 54 deletions src/Thybag/Auth/SharePointOnlineAuth.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* SharePointOnlineAuth
* Clone of the PHP SoapClient class, modified in order to allow transparent communication with
* the SharePoint Online Web services.
*
*
* @package Thybag\Auth
*/
class SharePointOnlineAuth extends \SoapClient {
Expand All @@ -15,60 +15,60 @@ class SharePointOnlineAuth extends \SoapClient {

// Override do request method
public function __doRequest($request, $location, $action, $version, $one_way = false) {

// Authenticate with SP online in order to get required authentication cookies
if (!$this->authCookies) $this->configureAuthCookies($location);

// Set base headers
$headers = array();
$headers[] = "Content-Type: text/xml;";

$curl = curl_init($location);

curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($curl, CURLOPT_POST, TRUE);

// Send request and auth cookies.
curl_setopt($curl, CURLOPT_POSTFIELDS, $request);
curl_setopt($curl, CURLOPT_COOKIE, $this->authCookies);
// Connection requires CURLOPT_SSLVERSION set to 3
curl_setopt($curl, CURLOPT_TIMEOUT, 10);
curl_setopt($curl, CURLOPT_SSLVERSION, 3);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);

// Useful for debugging
curl_setopt($curl, CURLOPT_VERBOSE,FALSE);
curl_setopt($curl, CURLOPT_HEADER, FALSE);
// SharePoint Online requires the SOAPAction header set for ADD/EDIT and DELETE Operations.
// Failure to have this will result in a "Security Validation exception"
// @see http://weblogs.asp.net/jan/archive/2009/05/25/quot-the-security-validation-for-this-page-is-invalid-quot-when-calling-the-sharepoint-web-services.aspx
if( strpos($request, 'UpdateListItems') !== FALSE ) {
$headers[] = 'SOAPAction: "http://schemas.microsoft.com/sharepoint/soap/UpdateListItems"';
}

// Add headers
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);

// Init the cURL
$response = curl_exec($curl);
// Throw exceptions if there are any issues
if (curl_errno($curl)) throw new \SoapFault(curl_error($curl));
if ($response == '') throw new \SoapFault("No XML returned", "No XML returned");
// Close CURL
curl_close($curl);
// Return?
if (!$one_way) return ($response);
$curl = curl_init($location);

curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($curl, CURLOPT_POST, TRUE);

// Send request and auth cookies.
curl_setopt($curl, CURLOPT_POSTFIELDS, $request);
curl_setopt($curl, CURLOPT_COOKIE, $this->authCookies);

// Connection requires CURLOPT_SSLVERSION set to 3
curl_setopt($curl, CURLOPT_TIMEOUT, 10);
curl_setopt($curl, CURLOPT_SSLVERSION, 3);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);

// Useful for debugging
curl_setopt($curl, CURLOPT_VERBOSE,FALSE);
curl_setopt($curl, CURLOPT_HEADER, FALSE);

// SharePoint Online requires the SOAPAction header set for ADD/EDIT and DELETE Operations.
// Failure to have this will result in a "Security Validation exception"
// @see http://weblogs.asp.net/jan/archive/2009/05/25/quot-the-security-validation-for-this-page-is-invalid-quot-when-calling-the-sharepoint-web-services.aspx
if( strpos($request, 'UpdateListItems') !== FALSE ) {
$headers[] = 'SOAPAction: "http://schemas.microsoft.com/sharepoint/soap/UpdateListItems"';
}

// Add headers
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);

// Init the cURL
$response = curl_exec($curl);

// Throw exceptions if there are any issues
if (curl_errno($curl)) throw new \SoapFault(curl_errno($curl), curl_error($curl));
if ($response == '') throw new \SoapFault("No XML returned", "No XML returned");

// Close CURL
curl_close($curl);

// Return?
if (!$one_way) return ($response);
}

/**
* ConfigureAuthCookies
* Authenticate with sharepoint online in order to get valid authentication cookies
*
*
* @param $location - Url of sharepoint list
*
* More info on method:
Expand Down Expand Up @@ -112,11 +112,11 @@ protected function configureAuthCookies($location) {
* extractAuthCookies
* Extract Authentication cookies from SP response & format in to usable cookie string
*
* @param $result cURL Response
* @param $result cURL Response
* @return $cookie_payload string containing cookie data.
*/
protected function extractAuthCookies($result){

$authCookies = array();
$cookie_payload = '';

Expand All @@ -129,7 +129,7 @@ protected function extractAuthCookies($result){
$authCookies[] = $loop[1];
}
}
unset($authCookies[0]); // No need for first cookie
unset($authCookies[0]); // No need for first cookie

// Extract cookie name & payload and format in to cURL compatible string
foreach($authCookies as $payload){
Expand Down Expand Up @@ -163,30 +163,30 @@ protected function authCurl($url, $payload, $header = false){
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_POST,1);
curl_setopt($ch,CURLOPT_POSTFIELDS, $payload);
curl_setopt($ch,CURLOPT_POSTFIELDS, $payload);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

curl_setopt($ch, CURLOPT_SSLVERSION, 3);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
if($header) curl_setopt($ch, CURLOPT_HEADER, true);

if($header) curl_setopt($ch, CURLOPT_HEADER, true);

$result = curl_exec($ch);

// catch error
if($result === false) {
throw new \SoapFault('Curl error: ' . curl_error($ch));
throw new \SoapFault(curl_errno($ch), 'Curl error: ' . curl_error($ch));
}

curl_close($ch);

return $result;
}

/**
* Get the XML to request the security token
*
*
* @param string $username
* @param string $password
* @param string $endpoint
Expand Down

0 comments on commit d4bfd3f

Please sign in to comment.