1 <?php
  2 /**
  3  * @package     Joomla.Platform
  4  * @subpackage  Linkedin
  5  *
  6  * @copyright   Copyright (C) 2005 - 2017 Open Source Matters, Inc. All rights reserved.
  7  * @license     GNU General Public License version 2 or later; see LICENSE
  8  */
  9 
 10 defined('JPATH_PLATFORM') or die();
 11 
 12 /**
 13  * Linkedin API Social Communications class for the Joomla Platform.
 14  *
 15  * @since  13.1
 16  */
 17 class JLinkedinCommunications extends JLinkedinObject
 18 {
 19     /**
 20      * Method used to invite people.
 21      *
 22      * @param   string  $email       A string containing email of the recipient.
 23      * @param   string  $first_name  A string containing frist name of the recipient.
 24      * @param   string  $last_name   A string containing last name of the recipient.
 25      * @param   string  $subject     The subject of the message that will be sent to the recipient
 26      * @param   string  $body        A text of the message.
 27      * @param   string  $connection  Only connecting as a 'friend' is supported presently.
 28      *
 29      * @return  array  The decoded JSON response
 30      *
 31      * @since   13.1
 32      */
 33     public function inviteByEmail($email, $first_name, $last_name, $subject, $body, $connection = 'friend')
 34     {
 35         $token = $this->oauth->getToken();
 36 
 37         // Set parameters.
 38         $parameters = array(
 39             'oauth_token' => $token['key'],
 40         );
 41 
 42         // Set the success response code.
 43         $this->oauth->setOption('success_code', 201);
 44 
 45         // Set the API base.
 46         $base = '/v1/people/~/mailbox';
 47 
 48         // Build the xml.
 49         $xml = '<mailbox-item>
 50                   <recipients>
 51                     <recipient>
 52                         <person path="/people/email=' . $email . '">
 53                             <first-name>' . $first_name . '</first-name>
 54                             <last-name>' . $last_name . '</last-name>
 55                         </person>
 56                     </recipient>
 57                 </recipients>
 58                 <subject>' . $subject . '</subject>
 59                 <body>' . $body . '</body>
 60                 <item-content>
 61                     <invitation-request>
 62                       <connect-type>' . $connection . '</connect-type>
 63                     </invitation-request>
 64                 </item-content>
 65              </mailbox-item>';
 66 
 67         $header['Content-Type'] = 'text/xml';
 68 
 69         // Build the request path.
 70         $path = $this->getOption('api.url') . $base;
 71 
 72         // Send the request.
 73         $response = $this->oauth->oauthRequest($path, 'POST', $parameters, $xml, $header);
 74 
 75         return $response;
 76     }
 77 
 78     /**
 79      * Method used to invite people.
 80      *
 81      * @param   string  $id          Member id.
 82      * @param   string  $first_name  A string containing frist name of the recipient.
 83      * @param   string  $last_name   A string containing last name of the recipient.
 84      * @param   string  $subject     The subject of the message that will be sent to the recipient
 85      * @param   string  $body        A text of the message.
 86      * @param   string  $connection  Only connecting as a 'friend' is supported presently.
 87      *
 88      * @return  array  The decoded JSON response
 89      *
 90      * @since   13.1
 91      */
 92     public function inviteById($id, $first_name, $last_name, $subject, $body, $connection = 'friend')
 93     {
 94         $token = $this->oauth->getToken();
 95 
 96         // Set parameters.
 97         $parameters = array(
 98             'oauth_token' => $token['key'],
 99         );
100 
101         // Set the API base for people search.
102         $base = '/v1/people-search:(people:(api-standard-profile-request))';
103 
104         $data['format'] = 'json';
105         $data['first-name'] = $first_name;
106         $data['last-name'] = $last_name;
107 
108         // Build the request path.
109         $path = $this->getOption('api.url') . $base;
110 
111         // Send the request.
112         $response = $this->oauth->oauthRequest($path, 'GET', $parameters, $data);
113 
114         if (strpos($response->body, 'apiStandardProfileRequest') === false)
115         {
116             throw new RuntimeException($response->body);
117         }
118 
119         // Get header value.
120         $value = explode('"value": "', $response->body);
121         $value = explode('"', $value[1]);
122         $value = $value[0];
123 
124         // Split on the colon character.
125         $value = explode(':', $value);
126         $name = $value[0];
127         $value = $value[1];
128 
129         // Set the success response code.
130         $this->oauth->setOption('success_code', 201);
131 
132         // Set the API base.
133         $base = '/v1/people/~/mailbox';
134 
135         // Build the xml.
136         $xml = '<mailbox-item>
137                   <recipients>
138                     <recipient>
139                         <person path="/people/id=' . $id . '">
140                         </person>
141                     </recipient>
142                 </recipients>
143                 <subject>' . $subject . '</subject>
144                 <body>' . $body . '</body>
145                 <item-content>
146                     <invitation-request>
147                       <connect-type>' . $connection . '</connect-type>
148                       <authorization>
149                         <name>' . $name . '</name>
150                         <value>' . $value . '</value>
151                       </authorization>
152                     </invitation-request>
153                 </item-content>
154              </mailbox-item>';
155 
156         $header['Content-Type'] = 'text/xml';
157 
158         // Build the request path.
159         $path = $this->getOption('api.url') . $base;
160 
161         // Send the request.
162         $response = $this->oauth->oauthRequest($path, 'POST', $parameters, $xml, $header);
163 
164         return $response;
165     }
166 
167     /**
168      * Method used to send messages via LinkedIn between two or more individuals connected to the member sending the message..
169      *
170      * @param   mixed   $recipient  A string containing the member id or an array of ids.
171      * @param   string  $subject    The subject of the message that will be sent to the recipient
172      * @param   string  $body       A text of the message.
173      *
174      * @return  array  The decoded JSON response
175      *
176      * @since   13.1
177      */
178     public function sendMessage($recipient, $subject, $body)
179     {
180         $token = $this->oauth->getToken();
181 
182         // Set parameters.
183         $parameters = array(
184             'oauth_token' => $token['key'],
185         );
186 
187         // Set the success response code.
188         $this->oauth->setOption('success_code', 201);
189 
190         // Set the API base.
191         $base = '/v1/people/~/mailbox';
192 
193         // Build the xml.
194         $xml = '<mailbox-item>
195                   <recipients>';
196 
197         if (is_array($recipient))
198         {
199             foreach ($recipient as $r)
200             {
201                 $xml .= '<recipient>
202                             <person path="/people/' . $r . '"/>
203                         </recipient>';
204             }
205         }
206 
207         $xml .= '</recipients>
208                  <subject>' . $subject . '</subject>
209                  <body>' . $body . '</body>
210                 </mailbox-item>';
211 
212         $header['Content-Type'] = 'text/xml';
213 
214         // Build the request path.
215         $path = $this->getOption('api.url') . $base;
216 
217         // Send the request.
218         $response = $this->oauth->oauthRequest($path, 'POST', $parameters, $xml, $header);
219 
220         return $response;
221     }
222 }
223