Skip to content

Commit eb44467

Browse files
committed
Merge pull request #47 from thybag/experimental
Roll experimental to live
2 parents a453784 + 390ecf3 commit eb44467

File tree

6 files changed

+305
-150
lines changed

6 files changed

+305
-150
lines changed

SharePointAPI.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
*
66
* Simple PHP API for reading/writing and modifying SharePoint list items.
77
*
8-
* @version 0.6.1
8+
* @version 0.6.2
99
* @licence MIT License
1010
* @source: http://github.com/thybag/PHP-SharePoint-Lists-API
1111
*

composer.json

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
{
2-
"name": "thybag/php-sharepoint-lists-api",
3-
"version": "0.6.1",
4-
"type": "library",
5-
"description": "A simple PHP API to make working with SharePoint lists easy.",
6-
"homepage": "https://github.com/thybag/PHP-SharePoint-Lists-API",
7-
"license": "MIT",
8-
"authors": [
9-
{
10-
"name": "Carl Saggs",
11-
"email": "[email protected]"
12-
}
13-
],
14-
"autoload": {
2+
"name": "thybag/php-sharepoint-lists-api",
3+
"version": "0.6.2",
4+
"type": "library",
5+
"description": "A simple PHP API to make working with SharePoint lists easy.",
6+
"homepage": "https://github.com/thybag/PHP-SharePoint-Lists-API",
7+
"license": "MIT",
8+
"authors": [
9+
{
10+
"name": "Carl Saggs",
11+
"email": "[email protected]"
12+
}
13+
],
14+
"autoload": {
1515
"psr-0": {
1616
"Thybag" : "src/"
1717
}
18-
}
18+
}
1919
}

readme.md

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,14 @@ or
102102
$sp->query('<list_name>')->where('surname', '=', 'smith')->limit(10)->sort('age','DESC')->get();
103103

104104

105+
###### To return the first 5 items, including the columns "favroite_cake" and "favorite animal"
106+
107+
$sp->read('<list_name>', 5, NULL, array("favroite_cake", "favorite_animal"));
108+
109+
or
110+
111+
$sp->query('<list_name>')->fields(array("favroite_cake", "favorite_animal")->limit(5)->get();
112+
105113

106114
By default list item's are returned as arrays with lower case index's. If you would prefer the results to return as object's, before invoking any read operations use:
107115

@@ -111,6 +119,7 @@ Automatically making the attribute names lowercase can also be deactivated by us
111119

112120
$sp->lowercaseIndexs(FALSE);
113121

122+
114123
#### Querying a list
115124
The query method can be used when you need to specify a query that is to complex to be easily defined using the read methods. Queries are constructed using a number of (hopefully expressive) pseudo SQL methods.
116125

@@ -122,6 +131,10 @@ If you wanted to get the first 10 pets that were either cats or hamsters you cou
122131

123132
$sp->query('list of pets')->where('type','=','cat')->or_where('type','=','hamster')->limit(10)->get();
124133

134+
If you need to return 5 items, but including all fields contained in a list, you can use. (pass false to all_fields to include hidden fields).
135+
136+
$sp->query('list of pets')->all_fields()->get();
137+
125138
If you have a set of CAML for a specific advanced query you would like to run, you can pass it to the query object using:
126139

127140
$sp->query('list of pets')->raw_where('<Eq><FieldRef Name="Title" /><Value Type="Text">Hello World</Value></Eq>')->limit(10)->get();
@@ -206,7 +219,7 @@ Files can be attached to SharePoint list items using:
206219

207220
The PHP SharePoint API contains a number of helper methods to make it easier to ensure certain values are in the correct format for some of SharePoints special data types.
208221

209-
#### dateTime
222+
###### dateTime
210223

211224
The dataTime method can either be passed a text based date
212225

@@ -218,7 +231,7 @@ Or a unix timestamp
218231

219232
And will return a value which can be stored in to SharePoints DateTime fields without issue.
220233

221-
#### Lookup
234+
###### Lookup
222235

223236
The lookup data type in SharePoint is for fields that reference a row in another list. In order to correctly populate these values you will need to know the ID of the row the value needs to reference.
224237

@@ -228,7 +241,7 @@ If you do not know the name/title of the value you are storing the method will w
228241

229242
$value = \Thybag\SharepointApi::lookup('3');
230243

231-
#### Magic Lookup
244+
###### Magic Lookup
232245

233246
If you are attempting to store a value in a "lookup" data type but for some reason only know the title/name of the item, not its ID, you can use the MagicLookup method to quickly look this value up and return it for you. This method will need to be passed both the items title & the list it is contained within.
234247

src/Thybag/Auth/SharePointOnlineAuth.php

Lines changed: 102 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* SharePointOnlineAuth
66
* Clone of the PHP SoapClient class, modified in order to allow transparent communication with
77
* the SharePoint Online Web services.
8-
*
8+
*
99
* @package Thybag\Auth
1010
*/
1111
class SharePointOnlineAuth extends \SoapClient {
@@ -15,60 +15,60 @@ class SharePointOnlineAuth extends \SoapClient {
1515

1616
// Override do request method
1717
public function __doRequest($request, $location, $action, $version, $one_way = false) {
18-
18+
1919
// Authenticate with SP online in order to get required authentication cookies
2020
if (!$this->authCookies) $this->configureAuthCookies($location);
21-
21+
2222
// Set base headers
2323
$headers = array();
2424
$headers[] = "Content-Type: text/xml;";
2525

26-
$curl = curl_init($location);
26+
$curl = curl_init($location);
27+
28+
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
29+
curl_setopt($curl, CURLOPT_POST, TRUE);
2730

28-
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
29-
curl_setopt($curl, CURLOPT_POST, TRUE);
31+
// Send request and auth cookies.
32+
curl_setopt($curl, CURLOPT_POSTFIELDS, $request);
33+
curl_setopt($curl, CURLOPT_COOKIE, $this->authCookies);
3034

31-
// Send request and auth cookies.
32-
curl_setopt($curl, CURLOPT_POSTFIELDS, $request);
33-
curl_setopt($curl, CURLOPT_COOKIE, $this->authCookies);
34-
35-
// Connection requires CURLOPT_SSLVERSION set to 3
36-
curl_setopt($curl, CURLOPT_TIMEOUT, 10);
37-
curl_setopt($curl, CURLOPT_SSLVERSION, 3);
38-
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
35+
// Connection requires CURLOPT_SSLVERSION set to 3
36+
curl_setopt($curl, CURLOPT_TIMEOUT, 10);
37+
curl_setopt($curl, CURLOPT_SSLVERSION, 3);
38+
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
3939

40-
// Useful for debugging
40+
// Useful for debugging
4141
curl_setopt($curl, CURLOPT_VERBOSE,FALSE);
42-
curl_setopt($curl, CURLOPT_HEADER, FALSE);
43-
44-
// SharePoint Online requires the SOAPAction header set for ADD/EDIT and DELETE Operations.
45-
// Failure to have this will result in a "Security Validation exception"
46-
// @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
47-
if( strpos($request, 'UpdateListItems') !== FALSE ) {
48-
$headers[] = 'SOAPAction: "http://schemas.microsoft.com/sharepoint/soap/UpdateListItems"';
49-
}
50-
51-
// Add headers
52-
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
53-
54-
// Init the cURL
55-
$response = curl_exec($curl);
56-
57-
// Throw exceptions if there are any issues
58-
if (curl_errno($curl)) throw new \SoapFault(curl_error($curl));
59-
if ($response == '') throw new \SoapFault("No XML returned");
60-
42+
curl_setopt($curl, CURLOPT_HEADER, FALSE);
43+
44+
// SharePoint Online requires the SOAPAction header set for ADD/EDIT and DELETE Operations.
45+
// Failure to have this will result in a "Security Validation exception"
46+
// @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
47+
if( strpos($request, 'UpdateListItems') !== FALSE ) {
48+
$headers[] = 'SOAPAction: "http://schemas.microsoft.com/sharepoint/soap/UpdateListItems"';
49+
}
50+
51+
// Add headers
52+
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
53+
54+
// Init the cURL
55+
$response = curl_exec($curl);
56+
57+
// Throw exceptions if there are any issues
58+
if (curl_errno($curl)) throw new \SoapFault('Receiver', curl_error($curl));
59+
if ($response == '') throw new \SoapFault('Receiver', "No XML returned");
60+
6161
// Close CURL
62-
curl_close($curl);
63-
64-
// Return?
65-
if (!$one_way) return ($response);
62+
curl_close($curl);
63+
64+
// Return?
65+
if (!$one_way) return ($response);
6666
}
6767

6868
/**
6969
* ConfigureAuthCookies
7070
* Authenticate with sharepoint online in order to get valid authentication cookies
71-
*
71+
*
7272
* @param $location - Url of sharepoint list
7373
*
7474
* More info on method:
@@ -93,59 +93,59 @@ protected function configureAuthCookies($location) {
9393

9494
// Extract security token from XML
9595
$xml = new \DOMDocument();
96-
$xml->loadXML($result);
97-
$xpath = new \DOMXPath($xml);
98-
$nodelist = $xpath->query("//wsse:BinarySecurityToken");
99-
foreach ($nodelist as $n){
100-
$token = $n->nodeValue;
101-
break;
102-
}
103-
104-
// Send token to SharePoint online in order to gain authentication cookies
105-
$result = $this->authCurl($endpoint."/_forms/default.aspx?wa=wsignin1.0", $token, true);
106-
107-
// Extract Authentication cookies from response & set them in to AuthCookies var
108-
$this->authCookies = $this->extractAuthCookies($result);
96+
$xml->loadXML($result);
97+
$xpath = new \DOMXPath($xml);
98+
$nodelist = $xpath->query("//wsse:BinarySecurityToken");
99+
foreach ($nodelist as $n){
100+
$token = $n->nodeValue;
101+
break;
102+
}
103+
104+
// Send token to SharePoint online in order to gain authentication cookies
105+
$result = $this->authCurl($endpoint."/_forms/default.aspx?wa=wsignin1.0", $token, true);
106+
107+
// Extract Authentication cookies from response & set them in to AuthCookies var
108+
$this->authCookies = $this->extractAuthCookies($result);
109109
}
110110

111111
/**
112112
* extractAuthCookies
113113
* Extract Authentication cookies from SP response & format in to usable cookie string
114114
*
115-
* @param $result cURL Response
115+
* @param $result cURL Response
116116
* @return $cookie_payload string containing cookie data.
117117
*/
118118
protected function extractAuthCookies($result){
119-
120-
$authCookies = array();
121-
$cookie_payload = '';
122-
123-
$header_array = explode("\r\n", $result);
124-
125-
// Get the two auth cookies
126-
foreach($header_array as $header) {
127-
$loop = explode(":",$header);
128-
if($loop[0] == 'Set-Cookie') {
129-
$authCookies[] = $loop[1];
130-
}
131-
}
132-
unset($authCookies[0]); // No need for first cookie
133-
134-
// Extract cookie name & payload and format in to cURL compatible string
135-
foreach($authCookies as $payload){
136-
$e = strpos($payload, "=");
137-
// Get name
138-
$name = substr($payload, 0, $e);
139-
// Get token
140-
$content = substr($payload, $e+1);
141-
$content = substr($content, 0, strpos($content, ";"));
142-
143-
// If not first cookie, add cookie seperator
144-
if($cookie_payload !== '') $cookie_payload .= '; ';
145-
146-
// Add cookie to string
147-
$cookie_payload .= $name.'='.$content;
148-
}
119+
120+
$authCookies = array();
121+
$cookie_payload = '';
122+
123+
$header_array = explode("\r\n", $result);
124+
125+
// Get the two auth cookies
126+
foreach($header_array as $header) {
127+
$loop = explode(":",$header);
128+
if($loop[0] == 'Set-Cookie') {
129+
$authCookies[] = $loop[1];
130+
}
131+
}
132+
unset($authCookies[0]); // No need for first cookie
133+
134+
// Extract cookie name & payload and format in to cURL compatible string
135+
foreach($authCookies as $payload){
136+
$e = strpos($payload, "=");
137+
// Get name
138+
$name = substr($payload, 0, $e);
139+
// Get token
140+
$content = substr($payload, $e+1);
141+
$content = substr($content, 0, strpos($content, ";"));
142+
143+
// If not first cookie, add cookie seperator
144+
if($cookie_payload !== '') $cookie_payload .= '; ';
145+
146+
// Add cookie to string
147+
$cookie_payload .= $name.'='.$content;
148+
}
149149

150150
return $cookie_payload;
151151
}
@@ -161,39 +161,39 @@ protected function extractAuthCookies($result){
161161
*/
162162
protected function authCurl($url, $payload, $header = false){
163163
$ch = curl_init();
164-
curl_setopt($ch,CURLOPT_URL,$url);
165-
curl_setopt($ch,CURLOPT_POST,1);
166-
curl_setopt($ch,CURLOPT_POSTFIELDS, $payload);
167-
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
168-
164+
curl_setopt($ch,CURLOPT_URL,$url);
165+
curl_setopt($ch,CURLOPT_POST,1);
166+
curl_setopt($ch,CURLOPT_POSTFIELDS, $payload);
167+
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
168+
169169
curl_setopt($ch, CURLOPT_SSLVERSION, 3);
170-
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
171-
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
172-
173-
if($header) curl_setopt($ch, CURLOPT_HEADER, true);
170+
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
171+
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
172+
173+
if($header) curl_setopt($ch, CURLOPT_HEADER, true);
174174

175-
$result = curl_exec($ch);
175+
$result = curl_exec($ch);
176176

177-
// catch error
178-
if($result === false) {
179-
throw new \SoapFault('Curl error: ' . curl_error($ch));
180-
}
181-
182-
curl_close($ch);
177+
// catch error
178+
if($result === false) {
179+
throw new \SoapFault('Sender', 'Curl error: ' . curl_error($ch));
180+
}
183181

184-
return $result;
182+
curl_close($ch);
183+
184+
return $result;
185185
}
186186

187187
/**
188188
* Get the XML to request the security token
189-
*
189+
*
190190
* @param string $username
191191
* @param string $password
192192
* @param string $endpoint
193193
* @return type string
194194
*/
195195
protected function generateSecurityToken($username, $password, $endpoint) {
196-
return <<<TOKEN
196+
return <<<TOKEN
197197
<s:Envelope xmlns:s="http://www.w3.org/2003/05/soap-envelope"
198198
xmlns:a="http://www.w3.org/2005/08/addressing"
199199
xmlns:u="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">

0 commit comments

Comments
 (0)