Odyssey
XRC_class.prg
1 <?php
2 
3  /* Possible Return Objects
4  value
5  'AliasInfo'
6  'EmailClientSummary'
7  'WebmailPresentation'
8  'Letter'
9  'MailboxSummary'
10  'FolderSummary'
11  'ForwardingInfo'
12  'AutoResponse'
13  'CreateUserOptions'
14  'UserAuthenticationInfo'
15  'ClientPreferences'
16  'WebMailPreferences'
17  'MatchingAccount'
18  */
19 
20 // *** END OF TESTING PORTION
21  /* -- CLASS FOR XRC-API
22  ** 8/15/2007 - MWS
23 
24 
25  ** PROPERTIES
26  dist_ID - This is the Distributor ID
27  dist_PWD - This is the Distributor Password
28 
29  XRC_Server - XRC Server part of the URL
30  XRC_Script - Script portion of the URL
31 
32  is_success - Was an error returned from the remote call? True/False
33  ** METHODS
34  */
35 
36 class XRC_API {
37  var $dist_ID;
38  var $dist_PWD;
39  var $XRC_Server;
40  var $XRC_Script;
41 
42  var $is_success;
43  var $XRC_FullData;
44 
45  var $Resp_version;
46  var $Resp_server;
47  var $Resp_status;
48  var $Resp_timestamp;
49  var $Resp_response;
50  var $Resp_Array;
51  var $Resp_ReturnObjectType;
52  var $Resp_ReturnObjectSize;
53 
54  var $values;
55 
56  var $ErrDesc;
57 
58  var $XRC_CMD = Array("version"=>"","clientID"=>"", "password"=>"", "cmd"=>"", "args"=>Array());
59 
60  function XRC_API_Method ($p_distID = '', $p_distPWD = '') {
61  // function XRC_API($p_distID = '', $p_distPWD = '') {
62  // * SET Default values
63  $this->XRC_CMD["version"] = "1";
64  $this->XRC_CMD["clientID"] = ($p_distID != '' ? $p_distID : '1646757');
65  $this->XRC_CMD["password"] = ($p_distPWD != '' ? $p_distPWD : 'tpw4tnms');
66 
67  $this->XRC_Server = "xrc.everyone.net";
68  $this->XRC_Script = "/ccc/xrc";
69 
70 
71  // $this->XRC_Server = "192.168.169.53";
72  // $this->XRC_Script = "/~mark/test_rec.php";
73  }
74 
75  function XRC_Parse () {
76 
77  // * The status should always return
78  /*
79  version: x
80  status: x
81  server: xyz
82  timestamp: ########
83  */
84  $pattern = "/version: ([\d]+)\nstatus: ([\d]+)\nserver: ([\w]+)\ntimestamp: ([\w]+)\n([^?]+)/";
85 
86  preg_match ($pattern, $this->XRC_FullData, $matches);
87 
88  /* Matches should fall into the following --
89  1 - version
90  2 - status
91  3 - server
92  4 - timestamp
93  5 - real response
94  */
95  $this->Resp_version = $matches[1];
96  $this->Resp_status = $matches[2];
97  $this->Resp_server = $matches[3];
98  $this->Resp_timestamp = $matches[4];
99  $this->Resp_response = $matches[5];
100 
101  // ** SET SUCCESS --- status = 0 then true else false
102  $this->is_success = ($this->Resp_status == 0 ? true : false);
103 
104  if ($this->is_success) {
105  // ** PASSED **
106  // Decipher the data
107  $this->XRC_StripNewLine();
108 
109  // $this->Resp_Array = explode("\n", $this->Resp_response);
110  $this->Resp_Array = explode("\n", $this->Resp_response);
111  // ** REMOVE a possible empty line at the end of the array -- caused by the explode of newlines
112  if ($this->Resp_Array[count($this->Resp_Array) - 1] == "") {
113  array_pop ($this->Resp_Array);
114  }
115 
116 
117  // $this->XRC_Decipher($this->Resp_response);
118  $this->values = $this->XRC_CreateList($this->Resp_Array);
119  // $this->XRC_MassageData($this->values);
120  } else {
121  // ** FAILED **
122  // Determine the error
123  $this->XRC_GetError();
124  }
125  print "<br>*** RESPONSE *** <bR>Version: {$this->Resp_version}<br>Status: {$this->Resp_status}<bR>Server: {$this->Resp_server}<br>Timestamp: {$this->Resp_timestamp}<br>Remainder: {$this->Resp_response}<br>";
126  }
127 
128  function clearArgs() {
129  // ** THIS SHOULD CLEAR THE ARRAY
130  $this->XRC_CMD["args"] = Array();
131  }
132 
133  function addArgs($type, $value) {
134  // ** Based on the type -- The value will get formatted differently
135  $arg_value = "";
136  if ($type == "I") {
137  // ** INTEGER
138  $arg_value = $value . "+";
139  } elseif ($type == "S") {
140  $arg_value = urlencode("\"$value\"") . "+";
141  }
142  $this->XRC_CMD["args"][] = $arg_value;
143  }
144 
145  function setCmd($value) {
146  $this->clearArgs();
147  $this->XRC_CMD['cmd'] = $value;
148  // ** Set the return Object Type -- based on the method
149  switch ($value) {
150  case "listClientIDsOfDistributor":
151  case "listUserNamesOfClient":
152  $this->Resp_ReturnObjectType = "value";
153  $this->Resp_ReturnObjectSize = "single";
154  break;
155  case "summarizeEmailClient":
156  $this->Resp_ReturnObjectType = "EmailClientSummary";
157  $this->Resp_ReturnObjectSize = "single";
158  break;
159  case "summarizeEmailClientsOfDistributor":
160  $this->Resp_ReturnObjectType = "EmailClientSummary";
161  $this->Resp_ReturnObjectSize = "list";
162  break;
163  case "summarizeUserMailbox":
164  $this->Resp_ReturnObjectType = "MailboxSummary";
165  $this->Resp_ReturnObjectSize = "single";
166  break;
167  default:
168  $this->Resp_ReturnObjectType = "value";
169  $this->Resp_ReturnObjectSize = "single";
170  break;
171  }
172  }
173 
174  function SendRequest($useHTTPS = true) {
175 
176  $this->is_success = false;
177  $this->XRC_FullData = "";
178  $this->ErrDesc = "";
179  $this->values = "";
180 
181  $url = "http" . ($useHTTPS ? "s" : "") . "://{$this->XRC_Server}{$this->XRC_Script}";
182 
183  fwrite ($fp, "<br> CMD - $cmd");
184 fclose($fp);
185 
186  $server_post = "";
187  $server_post .= "version=" . $this->XRC_CMD['version'];
188  $server_post .= "&clientID=" . $this->XRC_CMD['clientID'];
189  $server_post .= "&password=" . $this->XRC_CMD['password'];
190  $server_post .= "&cmd=" . $this->XRC_CMD['cmd'];
191 
192  $arg_string = "";
193  foreach ($this->XRC_CMD['args'] as $key => $value) {
194  // $arg_string .= (strlen($arg_string) > 0 ? " " : "") . $value;
195  $arg_string .= $value;
196  }
197  $server_post .= "&args=($arg_string)";
198 // $cmd = "/usr/bin/curl --silent --data-binary '$server_post' -H 'Content-Type: application/x-eon-xrc-response' $url";
199 
200 // $cmd = "/usr/bin/curl --silent --data-binary \"`echo -e 'version: 1\nclientID: 1646757\npassword: tpw4tnms\n\nnoop()'`\" -H 'Content-Type: application/x-eon-xrc-response' $url";
201 
202 // $cmd = "https://xrc.everyone.net/ccc/xrc?version=1&clientID=1646757&password=tpw4tnms&cmd=noop&args=()";
203 // $cmd = "https://xrc.everyone.net/ccc/xrc?version=1&clientID=1646757&password=tpw4tnms&cmd=getClientUserCap&args=(1647402+)";
204 // echo "https://xrc.everyone.net/ccc/xrc?version=1&clientID=1646757&password=tpw4tnms&cmd=getClientUserCap&args=(1647402+)";
205 
206 
207  $cmd = $url . "?" . $server_post;
208 // echo "<br>$cmd";
209 
210  // $fp = fopen ("/tmp/xrc_class.txt", "w+");
211 // // fwrite($fp, $cmd);
212 // // fclose($fp);
213 
214  // Open the file handle for curl
215  // $curl_ptr = popen($cmd, "r");
216  $curl_ptr = fopen($cmd, "r");
217  $curl_response = "";
218  if ($curl_ptr) {
219  do {
220  $curl_data = @fread($curl_ptr, 8192);
221  if (strlen($curl_data) == 0) {
222  break;
223  }
224  $curl_response .= $curl_data;
225  } while(true);
226 
227  // pclose($curl_ptr);
228  fclose($curl_ptr);
229  }
230 
231 
232  $fp = fopen ("/home/ubuntu/mark/xrc_class.txt", "a+");
233  fwrite ($fp, "Response - $curl_response");
234 // // fwrite($fp, $cmd);
235 fclose($fp);
236 
237  // fwrite($fp, $curl_response);
238  // fclose($fp);
239 
240  $this->XRC_FullData = $curl_response;
241  $this->XRC_Parse();
242  }
243  function XRC_GetError() {
244  // ** This will Set the Error description
245  $this->ErrDesc = $this->Resp_response;
246  }
247  function XRC_Decipher($pString) {
248  // ** Determine the type of object and set accordingly.....hmm...
249 
250  $retValue = "";
251 /*
252  if (substr($pString, 0, 1) == "/") {
253  // ** This is a single value such as /T, /F, /NULL -- strip the /
254  $retValue = substr($pString, 1);
255  } elseif (substr($pString, 0, 1) == "(") {
256  // ** This is a LIST --
257  // $pattern = "/[(\n]( [\d]+ )[\n)]/";
258 $pattern = "/( [\"]{0,1}[\w]+[\"]{0,1} \n)/";
259 // $pattern = "/[(\n]( [\d]+ )[\n)]/";
260 // print "<br>COUNT -- " . preg_match_all ($pattern, $pString, $matches) . "<bR>";
261  $retValue = $matches[1];
262 
263 
264 // print "MATCHES --- <br><br>" . print_r(array_values($matches[1])) . "<bR>";
265 // print "MATCHES --- " . $matches[0];
266  } else {
267  // ** TREAT THE RETURN VALUE AS THE VALUE
268  $retValue = $pString;
269  }
270 $pattern = "/( [\"]{0,1}[\w]+[\"]{0,1} \n)/";
271 $pattern = "/([^\^]+)\n/";
272 // print "<br>COUNT -- " . preg_match_all ($pattern, $pString, $matches) . "<bR>";
273 
274 */
275 
276  // ** Take the string -- break it into the an array of values based on the newline--- NOTE: the initial newlines should already be removed
277  $Value_List = explode ("\n", $pString);
278 
279  // ** NOW -- LOOP THROUGH THE RETURNED STRING -- Parse it into the "values" array
280 
281 // print "RETURN VALUE LIST " . count($Value_List);
282  return $Value_List;
283 
284 // print_r(array_values($Value_List));
285 // exit;
286 
287  /* **** DIFFERENT OBJECT TYPES
288  'value',
289  'AliasInfo',
290  'EmailClientSummary',
291  'WebmailPresentation',
292  'Letter',
293  'MailboxSummary',
294  'FolderSummary',
295  'ForwardingInfo',
296  'AutoResponse',
297  'CreateUserOptions',
298  'UserAuthenticationInfo',
299  'ClientPreferences',
300  'WebMailPreferences',
301  'MatchingAccount',
302  *** */
303  /* -- Need to allow the object to be single or within an array
304  '{single, list}'
305  */
306  switch ($this->Resp_ReturnObjectType) {
307  case "value":
308  // ** EACH entry is the entry in the array
309  $retValue = $this->XRC_MassageData($matches[1]);
310  break;
311  case "EmailClientSummary":
312 // echo "HERE<br><br><br>" . print_r(array_values($matches[0]));exit;
313 
314  default:
315  $this->is_success = false;
316  $this->ErrDesc = "Unknown Object Type";
317  break;
318  }
319  return $retValue;
320  }
321 
322  function XRC_MassageData($pData) {
323 
324  $pattern = "/[\"\/]/";
325  $replacewith = "";
326 
327 
328  $retValue = preg_replace ($pattern, $replacewith, $pData);
329  $retValue = trim($retValue);
330  // return preg_replace ($pattern, $replacewith, $pData);
331  return $retValue;
332  }
333 
334  function XRC_StripNewLine() {
335  // ** This will strip any newline from the beginning of the string
336  $cur_pos = 0;
337  while (substr($this->Resp_response, $cur_pos, 1) == "\n") {
338  $cur_pos++;
339  }
340  // ** If the position placement is greater than 0
341  if ($cur_pos > 0) {
342  $this->Resp_response = substr($this->Resp_response, $cur_pos);
343  }
344  }
345 
346 
347  function XRC_CreateList($pArray, $enumerate=false) {
348 
349 // echo "<Br><br>INSIDE CREATELIST - " . count($pArray) . ($enumerate ? "TRUE" : "FALSE");
350  $retValue = Array();
351  $partialArray = Array();
352  $level = 0;
353 
354  for ($idxArray = 0; $idxArray < count($pArray); $idxArray++) {
355  $useKey = "";
356  // $value = trim($pArray[$idxArray]); // ** REMOVE SPACES
357  $partialArray = Array();
358  $value = $this->XRC_MassageData($pArray[$idxArray]); // ** Remove "/" and quotes -- trim should be moved into here
359 // echo "<br>Print Value -- [$value]";
360 
361 
362  // ** IF WE ENUMERATE -- AND THIS is here -- then assume we are on the key
363  if ($enumerate) {
364  // print "<br>ENUMERATE --- KEY - $value";
365  // ** SET THE KEY TO USE
366  $useKey = $value;
367  $idxArray++;
368  // ** Advance the index to the next position
369  // $value = trim($pArray[$idxArray]); // ** REMOVE SPACES
370  $value = $this->XRC_MassageData($pArray[$idxArray]); // ** Remove "/" and quotes -- trim should be moved into here
371 
372  }
373 
374 //echo "<br><br>HERE --- [$value]";
375 
376  if (substr($value, 0, 1) == ":" || substr($value, 0, 1) == "(") {
377  // ** Preceded by a ":" refers to an object --
378  $level = 1;
379 
380  // Create an array that contains only the values for this OBJECT
381 
382  for ($partIdx = ($idxArray + 1); $partIdx < count($pArray); $partIdx++) {
383 
384  // ** Level is now 1 --- find the ")" that is also level one
385  // ** Until that time add each value to the partialArray variable
386  // $partValue = trim($pArray[$partIdx]); // ** REMOVE SPACES
387  $partValue = $this->XRC_MassageData($pArray[$partIdx]); // ** Remove "/" and quotes -- trim should be moved into here
388 
389  if ($level == 1 && $partValue == ")") {
390  break;
391  } elseif (strstr($partValue, ")") !== FALSE && $level > 1) {
392  $partialArray[] = $partValue;
393  $level--;
394  } elseif ((strstr($partValue, "(") !== FALSE) && ($partIdx == $idxArray)) {
395  // ** DO NOTHING
396  } elseif (strstr($partValue,"(") !== FALSE) {
397  $level++;
398  $partialArray[] = $partValue;
399  } else {
400  $partialArray[] = $partValue;
401  }
402 
403  }
404  $idxArray = $partIdx;
405  // ** We now have a partial array -- Now Recursive
406  // ** This is enumerated because it is a complex object
407 
408 // print "Start of recursion - [$value]";
409 // echo "<br><Br>PARTIAL LIST --- ";
410 // print_r(array_values($partialArray));
411 // echo "<br>" . count($partialArray) . " -- END PARTIAL<br><bR>";
412  if ($useKey == "") {
413  $retValue[] = $this->XRC_CreateList($partialArray, (substr($value, 0, 1) == ":" ? true : false));
414  } else {
415  $retValue[$useKey] = $this->XRC_CreateList($partialArray, (substr($value, 0, 1) == ":" ? true : false));
416  }
417 
418 // echo "<br>AFTER --- idx = $partIdx -- value " . $pArray[$partIdx];
419 
420  } else {
421  // *** For this type I want to add all the consecutive values without finding a "(" or ")" onto the list
422  if ($useKey == "") {
423 // echo "<br>Don't SET KEY";
424  $retValue[] = $pArray[$idxArray];
425  } else {
426 // echo "<br>SET KEY";
427  $retValue[$useKey] = $pArray[$idxArray];
428  }
429  }
430 
431  }
432  return $retValue;
433  }
434 
435 
436 
437 
438 
439 
440 
441 /*
442  function XRC_CreateList($pArray, $enumerate) {
443 
444  $retValue = Array();
445  $partialArray = Array();
446  $level = 0;
447  for ($idxArray = 0; $idxArray < count($pArray); $idxArray++) {
448 
449  // foreach ($pArray as $key=>$value) {
450  $value = trim($pArray[$idxArray]); // ** REMOVE SPACES
451  $value = XRC_MassageData($value); // ** Remove "/" and quotes -- trim should be moved into here
452 
453  switch (substr($value, 0, 1)
454 
455  if (substr($value, 0, 1) == ":") {
456  // ** Preceded by a ":" refers to an object --
457  $level = 1;
458  $retValue[] = XRC_CreateList($
459 
460  // Create an array that contains only the values for this OBJECT
461 
462  for ($partIdx = $idxArray; $partIdx < count($pArray); $partIdx++) {
463  // ** Level is now 1 --- find the ")" that is also level one
464  // ** Until that time add each value to the partialArray variable
465  $partValue = trim($pArray[$partIdx]); // ** REMOVE SPACES
466  $partValue = XRC_MassageData($partValue); // ** Remove "/" and quotes -- trim should be moved into here
467 
468  if ($level == 1 && $partValue == ")") {
469  break;
470  } elseif ($partValue == ")" && $level > 1) {
471  $level--;
472  } elseif ($partValue == "(") {
473  $level++;
474  }
475  $partialArray[] = $partValue;
476 
477  }
478  $idxArray = $partIdx;
479  // ** We now have a partial array -- Now Recursive
480  // ** This is enumerated because it is a complex object
481  $retValue[] = XRC_CreateList($partialArray, true) {
482 
483 
484  } elseif (substr($value, 0, 1) == "(") {
485  // ** beginning of a list
486  $level = 1;
487  for ($partIdx = $idxArray; $partIdx < count($pArray); $partIdx++) {
488  // ** Level is now 1 --- find the ")" that is also level one
489  // ** Until that time add each value to the partialArray variable
490  $partValue = trim($pArray[$partIdx]); // ** REMOVE SPACES
491  $partValue = XRC_MassageData($partValue); // ** Remove "/" and quotes -- trim should be moved into here
492 
493  if ($level == 1 && $partValue == ")") {
494  break;
495  } elseif ($partValue == ")" && $level > 1) {
496  $level--;
497  } elseif ($partValue == "(") {
498  $level++;
499  }
500  $partialArray[] = $partValue;
501 
502  }
503  $idxArray = $partIdx;
504  // ** We now have a partial array -- Now Recursive
505  // ** This is enumerated because it is a complex object
506  }
507 
508  }
509  }
510 */
511 }
512 ?>