PageRenderTime 72ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 1ms

/administrator/components/com_jfusion/models/model.curl.php

http://jfusion.googlecode.com/
PHP | 1524 lines | 1219 code | 80 blank | 225 comment | 202 complexity | 1f1274789a89827fbdc0311925f6a70c MD5 | raw file
Possible License(s): Apache-2.0

Large files files are truncated, but you can click here to view the full file

  1. <?php
  2. /**
  3. * curl login model
  4. *
  5. * PHP version 5
  6. *
  7. * @category JFusion
  8. * @package Models
  9. * @author Henk Wevers <henk@wevers.net>
  10. * @copyright 2008 - 2011 JFusion - Henk Wevers. All rights reserved.
  11. * @license http://www.gnu.org/copyleft/gpl.html GNU/GPL
  12. * @link http://www.jfusion.org
  13. */
  14. // no direct access
  15. defined('_JEXEC') or die('Restricted access');
  16. /**
  17. * HTML Form Parser
  18. * This will extract all forms and his elements in an
  19. * big assoc Array.
  20. * Many modifications and bug repairs by Henk Wevers
  21. *
  22. * @category JFusion
  23. * @package Models
  24. * @author Peter Valicek <Sonny2@gmx.DE>
  25. * @copyright 2004 Peter Valicek Peter Valicek <Sonny2@gmx.DE>: GPL-2
  26. * @license http://www.gnu.org/copyleft/gpl.html GNU/GPL
  27. * @link http://www.jfusion.org
  28. */
  29. class JFusionCurlHtmlFormParser
  30. {
  31. var $html_data = '';
  32. var $_return = array();
  33. var $_counter = '';
  34. var $button_counter = '';
  35. var $_unique_id = '';
  36. /**
  37. * html form parser
  38. *
  39. * @param string $html_data the actual html string
  40. *
  41. * @return array html elements
  42. */
  43. function JFusionCurlHtmlFormParser($html_data)
  44. {
  45. if (is_array($html_data)) {
  46. $this->html_data = join('', $html_data);
  47. } else {
  48. $this->html_data = $html_data;
  49. }
  50. $this->_return = array();
  51. $this->_counter = 0;
  52. $this->button_counter = 0;
  53. $this->_unique_id = md5(time());
  54. }
  55. /**
  56. * Parses the forms
  57. *
  58. * @return string nothing
  59. */
  60. function parseForms()
  61. {
  62. if (preg_match_all("/<form.*>.+<\/form>/isU", $this->html_data, $forms)) {
  63. foreach ($forms[0] as $form) {
  64. $this->button_counter = 0;
  65. //form details
  66. preg_match("/<form.*?name=[\"']?([\w\s-]*)[\"']?[\s>]/i", $form, $form_name);
  67. if ($form_name) {
  68. $this->_return[$this->_counter]['form_data']['name'] = preg_replace("/[\"'<>]/", "", $form_name[1]);
  69. }
  70. preg_match("/<form.*?action=(\"([^\"]*)\"|'([^']*)'|[^>\s]*)([^>]*)?>/i", $form, $action);
  71. if ($action) {
  72. $this->_return[$this->_counter]['form_data']['action'] = preg_replace("/[\"'<>]/", "", $action[1]);
  73. }
  74. preg_match("/<form.*?method=[\"']?([\w\s]*)[\"']?[\s>]/i", $form, $method);
  75. if ($method) {
  76. $this->_return[$this->_counter]['form_data']['method'] = preg_replace("/[\"'<>]/", "", $method[1]);
  77. }
  78. preg_match("/<form.*?enctype=(\"([^\"]*)\"|'([^']*)'|[^>\s]*)([^>]*)?>/i", $form, $enctype);
  79. if ($enctype) {
  80. $this->_return[$this->_counter]['form_data']['enctype'] = preg_replace("/[\"'<>]/", "", $enctype[1]);
  81. }
  82. preg_match("/<form.*?id=[\"']?([\w\s-]*)[\"']?[\s>]/i", $form, $id);
  83. if ($id) {
  84. $this->_return[$this->_counter]['form_data']['id'] = preg_replace("/[\"'<>]/", "", $id[1]);
  85. }
  86. // form elements: input type = hidden
  87. if (preg_match_all("/<input[^<>]+type=[\"']hidden[\"'][^<>]*>/iU", $form, $hiddens)) {
  88. foreach ($hiddens[0] as $hidden) {
  89. $this->_return[$this->_counter]['form_elements'][$this->_getName($hidden)] = array(
  90. 'type' => 'hidden',
  91. 'value' => $this->_getValue($hidden)
  92. );
  93. }
  94. }
  95. // form elements: input type = text
  96. if (preg_match_all("/<input[^<>]+type=[\"']text[\"'][^<>]*>/iU", $form, $texts)) {
  97. foreach ($texts[0] as $text) {
  98. $this->_return[$this->_counter]['form_elements'][$this->_getName($text)] = array(
  99. 'type' => 'text',
  100. 'value' => $this->_getValue($text),
  101. 'id' => $this->_getId($text),
  102. 'class' => $this->_getClass($text)
  103. );
  104. }
  105. }
  106. // form elements: input type = password
  107. if (preg_match_all("/<input[^<>]+type=[\"']password[\"'][^<>]*>/iU", $form, $passwords)) {
  108. foreach ($passwords[0] as $password) {
  109. $this->_return[$this->_counter]['form_elements'][$this->_getName($password)] = array(
  110. 'type' => 'password',
  111. 'value' => $this->_getValue($password)
  112. );
  113. }
  114. }
  115. // form elements: textarea
  116. if (preg_match_all("/<textarea.*>.*<\/textarea>/isU", $form, $textareas)) {
  117. foreach ($textareas[0] as $textarea) {
  118. preg_match("/<textarea.*>(.*)<\/textarea>/isU", $textarea, $textarea_value);
  119. $this->_return[$this->_counter]['form_elements'][$this->_getName($textarea)] = array(
  120. 'type' => 'textarea',
  121. 'value' => $textarea_value[1]
  122. );
  123. }
  124. }
  125. // form elements: input type = checkbox
  126. if (preg_match_all("/<input[^<>]+type=[\"']checkbox[\"'][^<>]*>/iU", $form, $checkboxes)) {
  127. foreach ($checkboxes[0] as $checkbox) {
  128. if (preg_match("/checked/i", $checkbox)) {
  129. $this->_return[$this->_counter]['form_elements'][$this->_getName($checkbox)] = array(
  130. 'type' => 'checkbox',
  131. 'value' => 'on'
  132. );
  133. } else {
  134. $this->_return[$this->_counter]['form_elements'][$this->_getName($checkbox)] = array(
  135. 'type' => 'checkbox',
  136. 'value' => ''
  137. );
  138. }
  139. }
  140. }
  141. // form elements: input type = radio
  142. if (preg_match_all("/<input[^<>]+type=[\"']radio[\"'][^<>]*>/iU", $form, $radios)) {
  143. foreach ($radios[0] as $radio) {
  144. if (preg_match("/checked/i", $radio)) {
  145. $this->_return[$this->_counter]['form_elements'][$this->_getName($radio)] = array(
  146. 'type' => 'radio',
  147. 'value' => $this->_getValue($radio)
  148. );
  149. }
  150. }
  151. }
  152. // form elements: input type = submit
  153. if (preg_match_all("/<input[^<>]+type=[\"']submit[\"'][^<>]*>/iU", $form, $submits)) {
  154. foreach ($submits[0] as $submit) {
  155. $this->_return[$this->_counter]['buttons'][$this->button_counter] = array(
  156. 'type' => 'submit',
  157. 'name' => $this->_getName($submit),
  158. 'value' => $this->_getValue($submit)
  159. );
  160. $this->button_counter++;
  161. }
  162. }
  163. // form elements: input type = button
  164. if (preg_match_all("/<input[^<>]+type=[\"']button[\"'][^<>]*>/iU", $form, $buttons)) {
  165. foreach ($buttons[0] as $button) {
  166. $this->_return[$this->_counter]['buttons'][$this->button_counter] = array(
  167. 'type' => 'button',
  168. 'name' => $this->_getName($button),
  169. 'value' => $this->_getValue($button)
  170. );
  171. $this->button_counter++;
  172. }
  173. }
  174. // form elements: input type = reset
  175. if (preg_match_all("/<input[^<>]+type=[\"']reset[\"'][^<>]*>/iU", $form, $resets)) {
  176. foreach ($resets[0] as $reset) {
  177. $this->_return[$this->_counter]['buttons'][$this->button_counter] = array(
  178. 'type' => 'reset',
  179. 'name' => $this->_getName($reset),
  180. 'value' => $this->_getValue($reset)
  181. );
  182. $this->button_counter++;
  183. }
  184. }
  185. // form elements: input type = image
  186. if (preg_match_all("/<input[^<>]+type=[\"']image[\"'][^<>]*>/iU", $form, $images)) {
  187. foreach ($images[0] as $image) {
  188. $this->_return[$this->_counter]['buttons'][$this->button_counter] = array(
  189. 'type' => 'image',
  190. 'name' => $this->_getName($image),
  191. 'value' => $this->_getValue($image)
  192. );
  193. $this->button_counter++;
  194. }
  195. }
  196. // input type=select entries
  197. // Here I have to go on step around to grep at first all select names and then
  198. // the content. Seems not to work in an other way
  199. if (preg_match_all("/<select.*>.+<\/select>/isU", $form, $selects)) {
  200. foreach ($selects[0] as $select) {
  201. if (preg_match_all("/<option.*>.+<\/option>/isU", $select, $all_options)) {
  202. foreach ($all_options[0] as $option) {
  203. if (preg_match("/selected/i", $option)) {
  204. if (preg_match("/value=[\"'](.*)[\"']\s/iU", $option, $option_value)) {
  205. $option_value = $option_value[1];
  206. $found_selected = 1;
  207. } else {
  208. preg_match("/<option.*>(.*)<\/option>/isU", $option, $option_value);
  209. $option_value = $option_value[1];
  210. $found_selected = 1;
  211. }
  212. }
  213. }
  214. if (!isset($found_selected)) {
  215. if (preg_match("/value=[\"'](.*)[\"']/iU", $all_options[0][0], $option_value)) {
  216. $option_value = $option_value[1];
  217. } else {
  218. preg_match("/<option>(.*)<\/option>/iU", $all_options[0][0], $option_value);
  219. $option_value = $option_value[1];
  220. }
  221. } else {
  222. unset($found_selected);
  223. }
  224. $this->_return[$this->_counter]['form_elements'][$this->_getName($select)] = array(
  225. 'type' => 'select',
  226. 'value' => trim($option_value)
  227. );
  228. }
  229. }
  230. }
  231. # form elements: input type = --not defined--
  232. if ( preg_match_all("/<input[^<>]+name=[\"'](.*)[\"'][^<>]*>/iU", $form, $inputs)) {
  233. foreach ( $inputs[0] as $input ) {
  234. if ( !preg_match("/type=(\"([^\"]*)\"|'([^']*)'|[^>\s]*)([^>]*)?>/is", $input) ) {
  235. if ( !isset($this->_return[$this->_counter]['form_elements'][$this->_getName($input)]) ) {
  236. $this->_return[$this->_counter]['form_elements'][$this->_getName($input)] =
  237. array(
  238. 'type' => 'text',
  239. 'value' => $this->_getValue($input),
  240. 'id' => $this->_getId($input),
  241. 'class' => $this->_getClass($input)
  242. );
  243. }
  244. }
  245. }
  246. }
  247. // Update the form counter if we have more then 1 form in the HTML table
  248. $this->_counter++;
  249. }
  250. }
  251. return $this->_return;
  252. }
  253. /**
  254. * gets the name
  255. *
  256. * @param string $string string
  257. *
  258. * @return string something
  259. */
  260. function _getName($string)
  261. {
  262. if (preg_match("/name=(\"([^\"]*)\"|'([^']*)'|[^>\s]*)([^>]*)?>/is", $string, $match)) {
  263. //preg_match("/name=[\"']?([\w\s]*)[\"']?[\s>]/i", $string, $match)) { -- did not work as expected
  264. $val_match = trim($match[1]);
  265. $val_match = trim($val_match, "\"\'");
  266. unset($string);
  267. return trim($val_match, '"');
  268. }
  269. }
  270. /**
  271. * gets the value
  272. *
  273. * @param string $string string
  274. *
  275. * @return string something
  276. */
  277. function _getValue($string)
  278. {
  279. if (preg_match("/value=(\"([^\"]*)\"|'([^']*)'|[^>\s]*)([^>]*)?>/is", $string, $match)) {
  280. $val_match = trim($match[1]);
  281. $val_match = trim($val_match, "\"\'");
  282. unset($string);
  283. return $val_match;
  284. }
  285. }
  286. /**
  287. * gets the id
  288. *
  289. * @param string $string string
  290. *
  291. * @return string something
  292. */
  293. function _getId($string)
  294. {
  295. if (preg_match("/id=(\"([^\"]*)\"|'([^']*)'|[^>\s]*)([^>]*)?>/is", $string, $match)) {
  296. //preg_match("/name=[\"']?([\w\s]*)[\"']?[\s>]/i", $string, $match)) { -- did not work as expected
  297. $val_match = trim($match[1]);
  298. $val_match = trim($val_match, "\"\'");
  299. unset($string);
  300. return $val_match;
  301. }
  302. }
  303. /**
  304. * gets the class
  305. *
  306. * @param string $string string
  307. *
  308. * @return string something
  309. */
  310. function _getClass($string)
  311. {
  312. if (preg_match("/class=(\"([^\"]*)\"|'([^']*)'|[^>\s]*)([^>]*)?>/is", $string, $match)) {
  313. $val_match = trim($match[1]);
  314. $val_match = trim($val_match, "\"\'");
  315. unset($string);
  316. return $val_match;
  317. }
  318. }
  319. }
  320. /**
  321. * Singleton static only class that creates instances for each specific JFusion plugin.
  322. *
  323. * @category JFusion
  324. * @package Models
  325. * @author Henk Wevers <henk@wevers.net>
  326. * @copyright 2008 JFusion - Henk Wevers. All rights reserved.
  327. * @license http://www.gnu.org/copyleft/gpl.html GNU/GPL
  328. * @link http://www.jfusion.org
  329. */
  330. class JFusionCurl
  331. {
  332. /**
  333. * NOTE: The routines buildcookie and imlodeCookies are identical to the ones in model.cookie.
  334. * They are duplicated here because I want this file selfcontained so it can be used by the DSSO routines in
  335. * the integrations. All you need to do there is to overload the JFusionCurl:: _ function to translate strings
  336. **/
  337. /**
  338. * Translate function, mimics the php gettext (alias _) function
  339. *
  340. * Do not overload when used within Joomla, the function simply calls Jtext::_
  341. * When you use the JFusionCurl class outside Joomla, f.i. as part of an DSSO extension in an integration
  342. * then you have to overload this function to provide the translated strings probably using native code
  343. *
  344. * @param string $string The string to translate
  345. * @param boolean $jsSafe Make the result javascript safe
  346. *
  347. * @return string The translation of the string
  348. **/
  349. public function _($string, $jsSafe = false)
  350. {
  351. return JText::_($string, $jsSafe);
  352. }
  353. /**
  354. * Retrieve the cookies as a string cookiename=cookievalue; or as an array
  355. *
  356. * @param string $type the type
  357. *
  358. * @return string or array
  359. */
  360. public function buildCookie($type = 'string')
  361. {
  362. switch ($type) {
  363. case 'array':
  364. return $_COOKIE;
  365. break;
  366. case 'string':
  367. default:
  368. return JFusionCurl::implodeCookies($_COOKIE, ';');
  369. break;
  370. }
  371. return false;
  372. }
  373. /**
  374. * Can implode an array of any dimension
  375. * Uses a few basic rules for implosion:
  376. * 1. Replace all instances of delimeters in strings by '/' followed by delimeter
  377. * 2. 2 Delimeters in between keys
  378. * 3. 3 Delimeters in between key and value
  379. * 4. 4 Delimeters in between key-value pairs
  380. *
  381. * @param array $array array
  382. * @param string $delimeter delemeter
  383. * @param string $keyssofar keyssofar
  384. *
  385. * @return string imploded cookies
  386. */
  387. function implodeCookies($array, $delimeter, $keyssofar = '')
  388. {
  389. $output = '';
  390. foreach ($array as $key => $value) {
  391. if (!is_array($value)) {
  392. if ($keyssofar) {
  393. $pair = $keyssofar . '[' . $key . ']=' . urlencode($value) . $delimeter;
  394. } else {
  395. $pair = $key . '=' . urlencode($value) . $delimeter;
  396. }
  397. if ($output != '') {
  398. $output .= ' ';
  399. }
  400. $output .= $pair;
  401. } else {
  402. if ($output != '') {
  403. $output .= ' ';
  404. }
  405. $output .= self::implodeCookies($value, $delimeter, $key . $keyssofar);
  406. }
  407. }
  408. return $output;
  409. }
  410. /**
  411. * curl redir exec
  412. *
  413. * @param string $ch ch
  414. *
  415. * @return string something
  416. */
  417. function curl_redir_exec($ch)
  418. {
  419. static $curl_loops = 0;
  420. static $curl_max_loops = 20;
  421. if ($curl_loops++ >= $curl_max_loops) {
  422. $curl_loops = 0;
  423. return false;
  424. }
  425. curl_setopt($ch, CURLOPT_HEADER, true);
  426. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  427. $data = curl_exec($ch);
  428. $lastdata = $data;
  429. $data = str_replace("\r", '', $data);
  430. list($header, $data) = explode("\n\n", $data, 2);
  431. $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  432. if ($http_code == 301 || $http_code == 302) {
  433. $matches = array();
  434. preg_match('/Location:(.*?)\n/', $header, $matches);
  435. $url = @parse_url(trim(array_pop($matches)));
  436. if (!$url) {
  437. //couldn't process the url to redirect to
  438. $curl_loops = 0;
  439. return $data;
  440. }
  441. $last_url = parse_url(curl_getinfo($ch, CURLINFO_EFFECTIVE_URL));
  442. /* if (!$url['scheme'])
  443. $url['scheme'] = $last_url['scheme'];
  444. if (!$url['host'])
  445. $url['host'] = $last_url['host'];
  446. if (!$url['path'])
  447. $url['path'] = $last_url['path'];
  448. */ $new_url = $url['scheme'] . '://' . $url['host'] . $url['path'] . ($url['query']?'?'.$url['query']:'');
  449. curl_setopt($ch, CURLOPT_URL, $new_url);
  450. return JFusionCurl::curl_redir_exec($ch);
  451. } else {
  452. $curl_loops=0;
  453. return $lastdata;
  454. }
  455. }
  456. /**
  457. * function read_header
  458. * Basic code was found on Svetlozar Petrovs website http://svetlozar.net/page/free-code.html.
  459. * The code is free to use and similar code can be found on other places on the net.
  460. *
  461. * @param string $ch ch
  462. * @param string $string string
  463. *
  464. * @return string something
  465. */
  466. function read_header($ch, $string)
  467. {
  468. global $location;
  469. global $cookiearr;
  470. global $ch;
  471. global $cookies_to_set;
  472. global $cookies_to_set_index;
  473. $length = strlen($string);
  474. if (!strncmp($string, "Location:", 9)) {
  475. $location = trim(substr($string, 9, -1));
  476. }
  477. if (!strncmp($string, "Set-Cookie:", 11)) {
  478. header($string, false);
  479. $cookiestr = trim(substr($string, 11, -1));
  480. $cookie = explode(';', $cookiestr);
  481. $cookies_to_set[$cookies_to_set_index] = $cookie;
  482. $cookies_to_set_index++;
  483. $cookie = explode('=', $cookie[0]);
  484. $cookiename = trim(array_shift($cookie));
  485. $cookiearr[$cookiename] = trim(implode('=', $cookie));
  486. }
  487. $cookie = "";
  488. if (!empty($cookiearr) && (trim($string) == "")) {
  489. foreach ($cookiearr as $key=>$value) {
  490. $cookie .= "$key=$value; ";
  491. }
  492. curl_setopt($ch, CURLOPT_COOKIE, $cookie);
  493. }
  494. return $length;
  495. }
  496. /**
  497. * function parseURL
  498. * out[0] = full url
  499. * out[1] = scheme or '' if no scheme was found
  500. * out[2] = username or '' if no auth username was found
  501. * out[3] = password or '' if no auth password was found
  502. * out[4] = domain name or '' if no domain name was found
  503. * out[5] = port number or '' if no port number was found
  504. * out[6] = path or '' if no path was found
  505. * out[7] = query or '' if no query was found
  506. * out[8] = fragment or '' if no fragment was found
  507. *
  508. * @param string $url url
  509. *
  510. * @return array output
  511. */
  512. function parseUrl($url)
  513. {
  514. $r = '!(?:(\w+)://)?(?:(\w+)\:(\w+)@)?([^/:]+)?';
  515. $r .= '(?:\:(\d*))?([^#?]+)?(?:\?([^#]+))?(?:#(.+$))?!i';
  516. preg_match($r, $url, $out);
  517. return $out;
  518. }
  519. /**
  520. * parses cookies
  521. *
  522. * @param array $cookielines cookies
  523. *
  524. * @return array parsed cookies
  525. */
  526. function parsecookies($cookielines)
  527. {
  528. $line=array();
  529. $cookies=array();
  530. foreach ($cookielines as $line) {
  531. $cdata = array();
  532. $data = array();
  533. foreach ($line as $data) {
  534. $cinfo = explode('=', $data);
  535. $cinfo[0] = trim($cinfo[0]);
  536. if (!isset($cinfo[1])) {
  537. $cinfo[1]='';
  538. }
  539. if (strcasecmp($cinfo[0], 'expires') == 0) {
  540. $cinfo[1] = strtotime($cinfo[1]);
  541. }
  542. if (strcasecmp($cinfo[0], 'secure') == 0) {
  543. $cinfo[1] = "true";
  544. }
  545. if (strcasecmp($cinfo[0], 'httponly') == 0) {
  546. $cinfo[1] = "true";
  547. }
  548. if (in_array(strtolower($cinfo[0]), array('domain', 'expires', 'path', 'secure', 'comment', 'httponly'))) {
  549. $cdata[trim($cinfo[0])] = $cinfo[1];
  550. } else {
  551. $cdata['value']['key'] = $cinfo[0];
  552. $cdata['value']['value'] = $cinfo[1];
  553. }
  554. }
  555. $cookies[] = $cdata;
  556. }
  557. return $cookies;
  558. }
  559. /**
  560. * Adds a cookie to the php header
  561. *
  562. * @param string $name cookie name
  563. * @param string $value cookie value
  564. * @param int $expires cookie expiry time
  565. * @param string $cookiepath cookie path
  566. * @param string $cookiedomain cookie domain
  567. * @param string $secure secure
  568. * @param string $httponly is the cookie http only
  569. * @param string $crossdomain_url cross domain url
  570. *
  571. * @return string nothing
  572. */
  573. function addCookie($name, $value='', $expires=0, $cookiepath='', $cookiedomain='', $secure=0, $httponly=0, $crossdomain_url='')
  574. {
  575. // Versions of PHP prior to 5.2 do not support HttpOnly cookies
  576. // IE is buggy when specifying a blank domain so set the cookie manually
  577. // solve the empty cookiedomain IE problem by specifying a domain in the plugin's parameters. <------
  578. if (version_compare(phpversion(), "5.2.0", ">=")) {
  579. setcookie($name, $value, $expires, $cookiepath, $cookiedomain, $secure, $httponly);
  580. } else {
  581. setcookie($name, $value, $expires, $cookiepath, $cookiedomain, $secure);
  582. }
  583. if ($crossdomain_url) {
  584. $jc = JFusionFactory::getCookies();
  585. $jc->addCookie($crossdomain_url, $name, $value, $expires, $cookiepath, $cookiedomain, $secure, $httponly);
  586. }
  587. }
  588. /**
  589. * sets my cookies
  590. *
  591. * @param string $status cookie name
  592. * @param string $mycookies_to_set cookie value
  593. * @param string $cookiedomain cookie domain
  594. * @param string $cookiepath cookie path
  595. * @param string $expires expires
  596. * @param string $secure secure
  597. * @param string $httponly is the cookie http only
  598. * @param string $crossdomain_url cross domain url
  599. *
  600. * @return string nothing
  601. */
  602. function setmycookies($status, $mycookies_to_set, $cookiedomain, $cookiepath, $expires=0, $secure=0, $httponly=1, $crossdomain_url='')
  603. {
  604. $cookies=array();
  605. $cookies=JFusionCurl::parsecookies($mycookies_to_set);
  606. foreach ($cookies as $cookie) {
  607. $name="";
  608. $value="";
  609. if ($expires == 0) {
  610. $expires_time=0;
  611. } else {
  612. $expires_time=time()+$expires;
  613. }
  614. if (isset($cookie['value']['key'])) {
  615. $name= $cookie['value']['key'];
  616. }
  617. if (isset($cookie['value']['value'])) {
  618. $value=$cookie['value']['value'];
  619. }
  620. if (isset($cookie['expires'])) {
  621. $expires_time=$cookie['expires'];
  622. }
  623. if (!$cookiepath) {
  624. if (isset($cookie['path'])) {
  625. $cookiepath=$cookie['path'];
  626. }
  627. }
  628. if (!$cookiedomain) {
  629. if (isset($cookie['domain'])) {
  630. $cookiedomain=$cookie['domain'];
  631. }
  632. }
  633. JFusionCurl::addCookie($name, urldecode($value), $expires_time, $cookiepath, $cookiedomain, $secure, $httponly, $crossdomain_url);
  634. if (($expires_time) == 0) {
  635. $expires_time='Session_cookie';
  636. } else {
  637. $expires_time=date('d-m-Y H:i:s', $expires_time);
  638. }
  639. $status['debug'][] = JFusionCurl::_('CREATED') . ' ' . JFusionCurl::_('COOKIE') . ': ' . JFusionCurl::_('NAME') . '=' . $name . ', ' . JFusionCurl::_('VALUE') . '=' . urldecode($value) .', ' .JFusionCurl::_('EXPIRES') . '=' .$expires_time .', ' . JFusionCurl::_('COOKIE_PATH') . '=' . $cookiepath . ', ' . JFusionCurl::_('COOKIE_DOMAIN') . '=' . $cookiedomain. ', '.JFusionCurl::_('COOKIE_SECURE') . '=' .$secure. ', '.JFusionCurl::_('COOKIE_HTTPONLY') . '=' .$httponly;
  640. if ($name=='MOODLEID_') {
  641. $status['cURL']['moodle'] = urldecode($value);
  642. }
  643. }
  644. return $status;
  645. }
  646. /**
  647. * delete my cookies
  648. *
  649. * @param string $status cookie name
  650. * @param string $mycookies_to_set cookie value
  651. * @param string $cookiedomain cookie domain
  652. * @param string $cookiepath cookie path
  653. * @param string $leavealone leavealone
  654. * @param string $secure secure
  655. * @param string $httponly is the cookie http only
  656. * @param string $crossdomain_url cross domain url
  657. *
  658. * @return string nothing
  659. */
  660. function deletemycookies($status, $mycookies_to_set, $cookiedomain, $cookiepath, $leavealone, $secure=0, $httponly=1, $crossdomain_url='')
  661. {
  662. $cookies=array();
  663. $cookies=JFusionCurl::parsecookies($mycookies_to_set);
  664. // leavealone keys/values while deleting
  665. // the $leavealone is an array of key=value that controls cookiedeletion
  666. // key = value
  667. // if key is an existing cookiename then that cookie will be affected depending on the value
  668. // if value = '>' then the 'name' cookies with an expiration date/time > now() will not be deleted
  669. // if value = '0' then the 'name' cookies will never be deleted at all
  670. // if name is a string than the cookie with that name will be affected
  671. // if name = '0' then all cookies will be affected according to the value
  672. // thus
  673. // MOODLEID_=> keeps the cookie with the name MOODLEID_ if expirationtime lies after now()
  674. // 0=> will keep all cookies that are not sessioncookies
  675. // 0=0 will keep all cookies
  676. if ($leavealone) {
  677. $leavealonearr = array();
  678. $lines = array();
  679. $line=array();
  680. $lines = explode(',', $leavealone);
  681. $i = 0;
  682. foreach ($lines as $line) {
  683. $cinfo = explode('=', $line);
  684. $leavealonearr[$i]['name'] = $cinfo[0];
  685. $leavealonearr[$i]['value'] = $cinfo[1];
  686. $i++;
  687. }
  688. }
  689. foreach ($cookies as $cookie) {
  690. // check if we schould leave the cookie alone
  691. $leaveit = false;
  692. if ($leavealone) {
  693. for ($i=0;$i<count($leavealonearr);$i++) {
  694. if (isset($cookie['value']['key'])) {
  695. if (($cookie['value']['key']== $leavealonearr[$i]['name']) || ($leavealonearr[$i]['name']=='0')) {
  696. if (($leavealonearr[$i]['value'] == '0')||($cookie['expires'] > time())) {
  697. $leaveit = true;
  698. }
  699. }
  700. }
  701. }
  702. }
  703. $name="";
  704. $value="";
  705. if (isset($cookie['value']['key'])) {
  706. $name= $cookie['value']['key'];
  707. }
  708. if (isset($cookie['expires'])) {
  709. $expires_time=$cookie['expires'];
  710. }
  711. if (!$cookiepath) {
  712. if (isset($cookie['path'])) {
  713. $cookiepath=$cookie['path'];
  714. }
  715. }
  716. if (!$cookiedomain) {
  717. if (isset($cookie['domain'])) {
  718. $cookiedomain=$cookie['domain'];
  719. }
  720. }
  721. if ($name=='MOODLEID_') {
  722. $status['cURL']['moodle'] = urldecode($cookie['value']['value']);
  723. }
  724. if (!$leaveit) {
  725. $expires_time=time()-30*60;
  726. $value = '';
  727. JFusionCurl::addCookie($name, urldecode($value), $expires_time, $cookiepath, $cookiedomain, $secure, $httponly, $crossdomain_url);
  728. if (($expires_time) == 0) {
  729. $expires_time='Session_cookie';
  730. } else {
  731. $expires_time=date('d-m-Y H:i:s', $expires_time);
  732. }
  733. $status['debug'][] = JFusionCurl::_('DELETED') . ' ' . JFusionCurl::_('COOKIE') . ': ' . JFusionCurl::_('NAME') . '=' . $name . ', ' . JFusionCurl::_('VALUE') . '=' . urldecode($value) .', ' .JFusionCurl::_('EXPIRES') . '=' .$expires_time .', ' . JFusionCurl::_('COOKIE_PATH') . '=' . $cookiepath . ', ' . JFusionCurl::_('COOKIE_DOMAIN') . '=' . $cookiedomain. ', '.JFusionCurl::_('COOKIE_SECURE') . '=' .$secure. ', '.JFusionCurl::_('COOKIE_HTTPONLY') . '=' .$httponly;
  734. } else {
  735. JFusionCurl::addCookie($name, urldecode($cookie['value']['value']), $expires_time, $cookiepath, $cookiedomain, $secure, $httponly, $crossdomain_url);
  736. if (($expires_time) == 0) {
  737. $expires_time='Session_cookie';
  738. } else {
  739. $expires_time=date('d-m-Y H:i:s', $expires_time);
  740. }
  741. $status['debug'][] = JFusionCurl::_('LEFT_ALONE') . ' ' . JFusionCurl::_('COOKIE') . ': ' . JFusionCurl::_('NAME') . '=' . $name . ', ' . JFusionCurl::_('VALUE') . '=' . urldecode($cookie['value']['value']) .', ' .JFusionCurl::_('EXPIRES') . '=' .$expires_time .', ' . JFusionCurl::_('COOKIE_PATH') . '=' . $cookiepath . ', ' . JFusionCurl::_('COOKIE_DOMAIN') . '=' . $cookiedomain. ', '.JFusionCurl::_('COOKIE_SECURE') . '=' .$secure. ', '.JFusionCurl::_('COOKIE_HTTPONLY') . '=' .$httponly;
  742. }
  743. }
  744. return $status;
  745. }
  746. /**
  747. * function ReadPage
  748. * This function will read a page of an integration
  749. * Caller should make sure that the Curl extension is loaded
  750. * @param array $curl_options curl options
  751. *
  752. * @return string page read
  753. */
  754. function ReadPage($curl_options, &$status, $curlinit=true)
  755. {
  756. global $ch;
  757. global $cookiearr;
  758. global $cookies_to_set;
  759. global $cookies_to_set_index;
  760. $cookies_to_set=array();
  761. $cookies_to_set_index=0;
  762. $open_basedir = ini_get('open_basedir');
  763. $safe_mode = ini_get('safe_mode');
  764. // read the page
  765. if ($curlinit) {
  766. $ch = curl_init();
  767. }
  768. curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
  769. curl_setopt($ch, CURLOPT_URL, $curl_options['post_url']);
  770. curl_setopt($ch, CURLOPT_REFERER, "");
  771. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
  772. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, $curl_options['verifyhost']);
  773. curl_setopt($ch, CURLOPT_FAILONERROR, 1);
  774. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  775. curl_setopt($ch, CURLOPT_VERBOSE, $curl_options['debug']); // Display communication with server
  776. if (empty($open_basedir) && empty($safe_mode)) {
  777. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
  778. }
  779. curl_setopt($ch, CURLOPT_MAXREDIRS, 2);
  780. curl_setopt($ch, CURLOPT_HEADERFUNCTION, array('JFusionCurl','read_header'));
  781. if (empty($curl_options['brute_force'])){
  782. curl_setopt($ch, CURLOPT_COOKIE, JFusionCurl::buildCookie());
  783. }
  784. if (!empty($curl_options['httpauth'])) {
  785. curl_setopt($ch, CURLOPT_USERPWD, "{$curl_options['httpauth_username']}:{$curl_options['httpauth_password']}");
  786. switch ($curl_options['httpauth']) {
  787. case "basic":
  788. $curl_options['httpauth'] = CURLAUTH_BASIC;
  789. break;
  790. case "gssnegotiate":
  791. $curl_options['httpauth'] = CURLAUTH_GSSNEGOTIATE;
  792. break;
  793. case "digest":
  794. $curl_options['httpauth'] = CURLAUTH_DIGEST;
  795. break;
  796. case "ntlm":
  797. $curl_options['httpauth'] = CURLAUTH_NTLM;
  798. break;
  799. case "anysafe":
  800. $curl_options['httpauth'] = CURLAUTH_ANYSAFE;
  801. break;
  802. case "any":
  803. default:
  804. $curl_options['httpauth'] = CURLAUTH_ANY;
  805. }
  806. curl_setopt($ch, CURLOPT_HTTPAUTH, $curl_options['httpauth']);
  807. }
  808. if (empty($open_basedir) && empty($safe_mode)) {
  809. $remotedata = curl_exec($ch);
  810. } else {
  811. $remotedata= JFusionCurl::curl_redir_exec($ch);
  812. }
  813. if ($curl_options['debug']) {
  814. $status['cURL']['data'][]= $remotedata;
  815. $status['debug'][]='CURL_INFO'.': '.print_r(curl_getinfo($ch), true);
  816. }
  817. if (curl_error($ch)) {
  818. $status['error'][] = JFusionCurl::_('CURL_ERROR_MSG').": ".curl_error($ch);
  819. curl_close($ch);
  820. return null;
  821. }
  822. if ($curl_options['integrationtype'] ==1) {
  823. curl_close($ch);
  824. }
  825. return $remotedata;
  826. }
  827. /**
  828. * function RemoteLogin
  829. * Smart function to programatically login to an JFusion integration
  830. * Will determine what to post (including, optionally, hidden form inputs) and what cookies to set.
  831. * Will then login.
  832. * In addition to username and password the function only needs an URL to a page with a loginform
  833. * and the ID of the loginform.
  834. * Including button information and hidden input posts is optionally
  835. *
  836. * 29-07-2011 Modified to handle logout as well when the loginform is used for logout
  837. * just call as login and add $curl_options['logout'] = '1'
  838. *
  839. * @param array $curl_options curl options
  840. *
  841. * @return string something
  842. */
  843. function RemoteLogin($curl_options)
  844. {
  845. global $ch;
  846. global $cookiearr;
  847. global $cookies_to_set;
  848. global $cookies_to_set_index;
  849. // extra lines for passing curl options to other routines, like ambrasubs payment processor
  850. // we are using the super global $_SESSION to pass data in $_SESSION[$var]
  851. $var = 'curl_options';
  852. if(!array_key_exists($var,$_SESSION)) $_SESSION[$var]='';
  853. $_SESSION[$var]=$curl_options;
  854. $GLOBALS[$var]=&$_SESSION[$var];
  855. // end extra lines
  856. $status = array();
  857. $tmpurl = array();
  858. $overridearr = array();
  859. $newhidden = array();
  860. $lines = array();
  861. $line=array();
  862. $cookies_to_set=array();
  863. $status['debug']=array();
  864. $status['error']=array();
  865. $status['cURL']=array();
  866. $status['cURL']['moodle']='';
  867. $status['cURL']['data']= array();
  868. $cookies_to_set_index=0;
  869. $open_basedir = ini_get('open_basedir');
  870. $safe_mode = ini_get('safe_mode');
  871. // check parameters and set defaults
  872. if (!isset($curl_options['integrationtype'])) {
  873. $curl_options['integrationtype'] = 1;
  874. }
  875. if (!isset($curl_options['relpath'])) {
  876. $curl_options['relpath'] = false;
  877. }
  878. if (!isset($curl_options['hidden'])) {
  879. $curl_options['hidden'] = false;
  880. }
  881. if (!isset($curl_options['buttons'])) {
  882. $curl_options['buttons'] = false;
  883. }
  884. if (!isset($curl_options['override'])) {
  885. $curl_options['override'] = null;
  886. }
  887. if (!isset($curl_options['cookiedomain'])) {
  888. $curl_options['cookiedomain'] = '';
  889. }
  890. if (!isset($curl_options['cookiepath'])) {
  891. $curl_options['cookiepath'] = '';
  892. }
  893. if (!isset($curl_options['expires'])) {
  894. $curl_options['expires'] = 1800;
  895. }
  896. if (!isset($curl_options['input_username_id'])) {
  897. $curl_options['input_username_id'] = '';
  898. }
  899. if (!isset($curl_options['input_password_id'])) {
  900. $curl_options['input_password_id'] = '';
  901. }
  902. if (!isset($curl_options['secure'])) {
  903. $curl_options['secure'] = 0;
  904. }
  905. if (!isset($curl_options['httponly'])) {
  906. $curl_options['httponly'] = 0;
  907. }
  908. if (!isset($curl_options['verifyhost'])) {
  909. $curl_options['verifyhost'] = 1;
  910. }
  911. if (!isset($curl_options['crossdomain_url'])) {
  912. $curl_options['crossdomain_url'] = '';
  913. }
  914. if (!isset($curl_options['debug'])) {
  915. $curl_options['debug'] = false;
  916. }
  917. // find out if we have a SSL enabled website
  918. if (strpos($curl_options['post_url'], 'https://') === false) {
  919. $ssl_string = 'http://';
  920. } else {
  921. $ssl_string = 'https://';
  922. }
  923. // check if curl extension is loaded
  924. if (!isset($curl_options['post_url']) || !isset($curl_options['formid'])) {
  925. $status['error'][] = JFusionCurl::_('CURL_FATAL');
  926. return $status;
  927. }
  928. if (!extension_loaded('curl')) {
  929. $status['error'][] = JFusionCurl::_('CURL_NOTINSTALLED');
  930. return $status;
  931. }
  932. $status['debug'][] = JFusionCurl::_('CURL_POST_URL_1')." ".$curl_options['post_url'];
  933. $remotedata = JFusionCurl::ReadPage($curl_options, $status,true);
  934. if (!empty($status['error'])) {
  935. return $status;
  936. }
  937. $status['debug'][] = JFusionCurl::_('CURL_PHASE_1');
  938. $status1=JFusionCurl::setmycookies($status, $cookies_to_set, $curl_options['cookiedomain'], $curl_options['cookiepath'], $curl_options['expires'], $curl_options['secure'], $curl_options['httponly'], $curl_options['crossdomain_url']);
  939. $status = array_merge($status,$status1);
  940. //find out if we have the form with the name/id specified
  941. $parser = new JFusionCurlHtmlFormParser($remotedata);
  942. $result = $parser->parseForms();
  943. $frmcount = count($result);
  944. $myfrm = -1;
  945. $i = 0;
  946. do {
  947. if (isset($result[$i]['form_data']['name'])) {
  948. if ($result[$i]['form_data']['name']==$curl_options['formid']) {
  949. $myfrm = $i;
  950. break;
  951. }
  952. }
  953. if (isset($result[$i]['form_data']['id'])) {
  954. if ($result[$i]['form_data']['id']==$curl_options['formid']) {
  955. $myfrm = $i;
  956. break;
  957. }
  958. }
  959. if (isset($result[$i]['form_data']['action'])) {
  960. if ($result[$i]['form_data']['action']==$curl_options['formid']) {
  961. $myfrm = $i;
  962. break;
  963. }
  964. }
  965. $i +=1;
  966. } while ($i<$frmcount);
  967. if ($myfrm == -1) {
  968. $helpthem = '';
  969. if ($frmcount >0) {
  970. $i = 0;
  971. $helpthem = 'I found';
  972. do {
  973. if (isset($result[$i]['form_data']['id'])) {
  974. $helpthem = $helpthem.' -- Name='.$result[$i]['form_data']['name'].' &ID='.$result[$i]['form_data']['id'];
  975. }
  976. $i +=1;
  977. } while ($i<$frmcount);
  978. }
  979. $status['error'][] = JFusionCurl::_('CURL_NO_LOGINFORM')." ".$helpthem;
  980. return $status;
  981. }
  982. $status['debug'][] = JFusionCurl::_('CURL_VALID_FORM');
  983. // by now we have the specified login/logout form, lets get the data needed to login/logout
  984. // we went to all this trouble to get to the hidden input entries.
  985. // The stuff is there to enhance security and is, yes, hidden
  986. $form_action = htmlspecialchars_decode($result[$myfrm]['form_data']['action']);
  987. $form_method = $result[$myfrm]['form_data']['method'];
  988. $elements_keys = array_keys($result[$myfrm]['form_elements']);
  989. $elements_values = array_values($result[$myfrm]['form_elements']);
  990. $elements_count = count($result[$myfrm]['form_elements']);
  991. // override keys/values from hidden inputs
  992. // the $override is an array of keys/values that override existing keys/values
  993. if (empty($curl_options['logout'])){
  994. if ($curl_options['override']) {
  995. $lines = explode(',', $curl_options['override']);
  996. foreach ($lines as $line) {
  997. $cinfo = explode('=', $line);
  998. $overridearr[$cinfo[0]]['value'] = $cinfo[1];
  999. $overridearr[$cinfo[0]]['type'] = 'hidden';
  1000. }
  1001. $newhidden= array_merge($result[$myfrm]['form_elements'], $overridearr);
  1002. $elements_keys = array_keys($newhidden);
  1003. $elements_values = array_values($newhidden);
  1004. $elements_count = count($newhidden);
  1005. }
  1006. }
  1007. // now construct the action parameter
  1008. // we have 4 possible options:
  1009. // case 0 Form action is without httpo.. and relpath = 0 , special case
  1010. // case 1 Form action is without http.. and relpath = 1 , just construct the action
  1011. // case 2 form_action is a full url, eg http..... and relpath = 0 This is easy, we do nothing at all
  1012. // case 3 form_action is a full url, eg http..... and relpath = 1 special case
  1013. $rel = (int)($curl_options['relpath']);
  1014. // if (substr($form_action,0,strlen($ssl_string))== $ssl_string) $hashttp = 2; else $hashttp = 0;
  1015. if (substr($form_action, 0, strlen('http'))== 'http') {
  1016. $hashttp = 2;
  1017. } else {
  1018. $hashttp = 0;
  1019. }
  1020. switch($rel+$hashttp) {
  1021. case 0:
  1022. //add a / in front of form_action
  1023. if (substr($form_action, 0, 1) != "/") {
  1024. $form_action = '/'.$form_action;
  1025. }
  1026. // we need to correct various situations like
  1027. // relative url from basedir, relative url from postdir etc
  1028. $tmpurl = JFusionCurl::parseUrl($curl_options['post_url']);
  1029. $pathinfo1 = pathinfo($form_action);
  1030. $pathinfo = pathinfo($tmpurl[6]);
  1031. //$status['debug'][] = 'post_url : '.print_r($curl_options['post_url'],true);
  1032. //$status['debug'][] = 'tmpurl : '.print_r($tmpurl,true);
  1033. //$status['debug'][] = 'form_action: '.print_r($form_action,true);
  1034. //$status['debug'][] = 'pathinfo1 : '.print_r($pathinfo1,true);
  1035. //$status['debug'][] = 'pathinfo : '.print_r($pathinfo,true);
  1036. if ($pathinfo['dirname'] == $pathinfo1['dirname']) {
  1037. $pathinfo['dirname']='';
  1038. } //prevent double directory
  1039. // replace windows DS bt unix DS
  1040. $pathinfo['dirname'] = str_replace("\\", "/", $pathinfo['dirname']);
  1041. // get rid of the trailing / in dir
  1042. rtrim($pathinfo['dirname'], '/');
  1043. $port = !empty($tmpurl[5]) ? ":".$tmpurl[5] : '';
  1044. $form_action = $ssl_string.$tmpurl[4].$port.$pathinfo['dirname'].$form_action;
  1045. //$status['debug'][] = 'form_action_final: '.print_r($form_action,true);
  1046. break;
  1047. case 1:
  1048. //add a / in front of form_action
  1049. if (substr($form_action, 0, 1) != "/") {
  1050. $form_action = '/'.$form_action;
  1051. }
  1052. $curl_options['post_url']=rtrim($curl_options['post_url'], '/');
  1053. $form_action = $curl_options['post_url'].$form_action;
  1054. break;
  1055. case 2:
  1056. //do nothing at all
  1057. break;
  1058. case 3:
  1059. // reserved, maybe something pops up, then we use this
  1060. break;
  1061. }
  1062. if (empty($curl_options['logout'])){
  1063. $input_username_name="";
  1064. for ($i = 0; $i <= $elements_count-1; $i++) {
  1065. if ($curl_options['input_username_id']) {
  1066. if (strtolower($elements_keys[$i]) == strtolower($curl_options['input_username_id'])) {
  1067. $input_username_name=$elements_keys[$i];
  1068. break;
  1069. }
  1070. }
  1071. if ($input_username_name == "") {
  1072. if (strpos(strtolower($elements_keys[$i]), 'user')!==false) {
  1073. $input_username_name=$elements_keys[$i];
  1074. }
  1075. if (strpos(strtolower($elements_keys[$i]), 'name')!==false) {
  1076. $input_username_name=$elements_keys[$i];
  1077. }
  1078. }
  1079. }
  1080. if ($input_username_name == "") {
  1081. $status['error'][] = JFusionCurl::_('CURL_NO_NAMEFIELD');
  1082. return $status;
  1083. }
  1084. $input_password_name = "";
  1085. for ($i = 0; $i <= $elements_count-1; $i++) {
  1086. if ($curl_options['input_password_id']) {
  1087. if (strtolower($elements_keys[$i]) == strtolower($curl_options['input_password_id'])) {
  1088. $input_password_name=$elements_keys[$i];
  1089. break;
  1090. }
  1091. }
  1092. if (strpos(strtolower($elements_keys[$i]), 'pass')!==false) {
  1093. $input_password_name=$elements_keys[$i];
  1094. }
  1095. }
  1096. if ($input_password_name=="") {
  1097. $status['error'][] = JFusionCurl::_('CURL_NO_PASSWORDFIELD');
  1098. return $status;
  1099. }
  1100. $status['debug'][] = JFusionCurl::_('CURL_VALID_USERNAME');
  1101. }
  1102. // we now set the submit parameters. These are:
  1103. // all form_elements name=value combinations with value != '' and type hidden
  1104. $strParameters="";
  1105. if ($curl_options['hidden']) {
  1106. for ($i = 0; $i <= $elements_count-1; $i++) {
  1107. if (($elements_values[$i] ['value'] != '')&& ($elements_values[$i] ['type'] == 'hidden')) {
  1108. $strParameters .= '&'.$elements_keys[$i].'='.urlencode($elements_values[$i] ['value']);
  1109. }
  1110. }
  1111. }
  1112. // code for buttons submitted by Daniel Baur
  1113. if ($curl_options['buttons']) {
  1114. if (isset($result[$myfrm] ['buttons'][0]['type'])) {
  1115. if ($result[$myfrm] ['buttons'][0]['type'] =='submit') {
  1116. if ($result[$myfrm]['buttons'][0]['name']) {
  1117. $strParameters .= '&'.$result[$myfrm]['buttons'][0]['name'].'='.urlencode($result[$myfrm] ['buttons'][0]['value']);
  1118. } else {
  1119. $strParameters .= '&'.'submit'.'='.urlencode($result[$myfrm] ['buttons'][0]['value']);
  1120. }
  1121. }
  1122. }
  1123. }
  1124. // extra post parameter to avoid endless loop when more then one jFusion is installed
  1125. if (isset($curl_options['jnodeid'])) {
  1126. $strParameters .= '&jnodeid='.urlencode($curl_options['jnodeid']);
  1127. }
  1128. // extra post parameter to signal a host calling
  1129. if (isset($curl_options['jhost'])) {
  1130. $strParameters .= '&jhost=true';
  1131. }
  1132. if (empty($curl_options['logout'])){
  1133. $post_params = $input_username_name."=".urlencode($curl_options['username'])."&".$input_password_name."=".urlencode($curl_options['password']);
  1134. $post_params_debug = $input_username_name."=".urlencode($curl_options['username'])."&".$input_password_name."=xxxxxx";
  1135. $status['debug'][] = JFusionCurl::_('CURL_STARTING_LOGIN')." ".$form_action." parameters= ".$post_params_debug.$strParameters;
  1136. } else {
  1137. $post_params = '';
  1138. $status['debug'][] = JFusionCurl::_('CURL_STARTING_LOGOUT')." ".$form_action." parameters= ".$strParameters;
  1139. }
  1140. // finally submit the login/logout form:
  1141. if ($curl_options['integrationtype'] == 1) {
  1142. $ch = curl_init();
  1143. curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
  1144. curl_setopt($ch, CURLOPT_REFERER, "");
  1145. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
  1146. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, $curl_options['verifyhost']);
  1147. curl_setopt($ch, CURLOPT_FAILONERROR, 1);
  1148. curl_setopt($ch, CURLOPT_HEADERFUNCTION, array('JFusionCurl','read_header'));
  1149. if (empty($curl_options['brute_force'])){
  1150. curl_setopt($ch, CURLOPT_COOKIE, JFusionCurl::buildCookie());
  1151. }
  1152. curl_setopt($ch, CURLOPT_VERBOSE, $curl_options['debug']); // Display communication with server
  1153. }
  1154. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);
  1155. curl_setopt($ch, CURLOPT_URL, $form_action);
  1156. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  1157. curl_setopt($ch, CURLOPT_POST, 1);
  1158. curl_setopt($ch, CURLOPT_POSTFIELDS, $post_params.$strParameters);
  1159. if (!empty($curl_options['httpauth'])) {
  1160. curl_setopt($ch, CURLOPT_USERPWD, "{$curl_options['httpauth_username']}:{$curl_options['httpauth_password']}");
  1161. curl_setopt($ch, CURLOPT_HTTPAUTH, $curl_options['httpauth']);
  1162. }
  1163. $remotedata = curl_exec($ch);
  1164. if ($curl_options['debug']) {
  1165. $status['cURL']['data'][]= $remotedata;
  1166. $status['debug'][]='CURL_INFO'.': '.print_r(curl_getinfo($ch), true);
  1167. }
  1168. if (curl_error($ch)) {
  1169. $status['error'][] = JFusionCurl::_('CURL_ERROR_MSG').": ".curl_error($ch);
  1170. curl_close($ch);
  1171. return $status;
  1172. }
  1173. curl_close($ch);
  1174. //we have to set the cookies now
  1175. if (empty($curl_options['logout'])){
  1176. $status['debug'][] = JFusionCurl::_('CURL_LOGIN_FINISHED');
  1177. $status=JFusionCurl::setmycookies($status, $cookies_to_set, $curl_options['cookiedomain'], $curl_options['cookiepath'], $curl_options['expires'], $curl_options['secure'], $curl_options['httponly'], $curl_options['crossdomain_url']);
  1178. } else {
  1179. $status['debug'][] = JFusionCurl::_('CURL_LOGOUT_FINISHED');
  1180. $status=JFusionCurl::deletemycookies($status, $cookies_to_set, $curl_options['cookiedomain'], $curl_options['cookiepath'], $curl_options['expires'], $curl_options['secure'], $curl_options['httponly'], $curl_options['crossdomain_url']);
  1181. }
  1182. $cookies_to_set_index = 0;
  1183. return $status;
  1184. }
  1185. /**
  1186. * RemoteLogout
  1187. *
  1188. * @param array $curl_options curl options
  1189. *
  1190. * @return string something
  1191. */
  1192. function RemoteLogout($curl_options)
  1193. {
  1194. $status=array();
  1195. global $ch;
  1196. global $cookiearr;
  1197. global $cookies_to_set;
  1198. global $cookies_to_set_index;
  1199. $tmpurl = array();
  1200. $cookies_to_set=array();
  1201. $cookies_to_set_index=0;
  1202. $status['debug']=array();
  1203. $status['error']=array();
  1204. $status['cURL']=array();
  1205. $status['cURL']['moodle']='';
  1206. $status['cURL']['data']= array();
  1207. // check parameters and set defaults
  1208. if (!isset($curl_options['post_url'])) {
  1209. $status['error'][]= 'Fatal programming error : no post_url!';
  1210. return $status;
  1211. }
  1212. if (!isset($curl_options['cookiedomain'])) {
  1213. $curl_options['cookiedomain'] = '';
  1214. }
  1215. if (!isset($curl_options['cookiepath'])) {
  1216. $curl_options['cookiepath'] = '';
  1217. }
  1218. if (!isset($curl_options['leavealone'])) {
  1219. $curl_options['leavealone'] = null;
  1220. }
  1221. if (!isset($curl_options['secure'])) {
  1222. $curl_options['secure'] = 0;
  1223. }
  1224. if (!isset($curl_options['httponly'])) {
  1225. $curl_options['httponly'] = 0;
  1226. }
  1227. if (!isset($curl_options['verifyhost'])) {
  1228. $curl_options['verifyhost'] = 1;
  1229. }
  1230. if (!isset($curl_options['crossdomain_url'])) {
  1231. $curl_options['crossdomain_url'] = '';
  1232. }
  1233. if (!isset($curl_options['debug'])) {
  1234. $curl_options['debug'] = false;
  1235. }
  1236. // prevent usererror by not supplying trailing backslash.
  1237. // make sure that when parameters are sent we do not add a backslash
  1238. if (strpos($curl_options['post_url'], '?') === false) {
  1239. if (!(substr($curl_options['post_url'], -1) == "/")) {
  1240. $curl_options['post_url'] = $curl_options['post_url']."/";
  1241. }
  1242. }
  1243. $ch = curl_init();
  1244. curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
  1245. curl_setopt($ch, CURLOPT_REFERER, "");
  1246. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
  1247. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, $curl_options['verifyhost']);
  1248. curl_setopt($ch, CURLOPT_FAILONERROR, 1);
  1249. curl_setopt($ch, CURLOPT_HEADERFUNCTION, array('JFusionCurl','read_header'));
  1250. curl_setopt($ch, CURLOPT_URL, $curl_options['post_url']);
  1251. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  1252. curl_setopt($ch, CURLOPT_VERBOSE, $curl_options['debug']); // Display communication with server
  1253. if (!empty($curl_options['httpauth'])) {
  1254. curl_setopt($ch, CURLOPT_USERPWD, "{$curl_options['httpauth_username']}:{$curl_options['httpauth_password']}");
  1255. switch ($curl_options['httpauth']) {
  1256. case "basic":
  1257. $curl_options['httpauth'] = CURLAUTH_BASIC;
  1258. break;
  1259. case "gssnegotiate":
  1260. $curl_options['httpauth'] = CURLAUTH_GSSNEGOTIATE;
  1261. break;
  1262. case "digest":
  1263. $curl_options['httpauth'] = CURLAUTH_DIGEST;
  1264. break;
  1265. case "ntlm":
  1266. $curl_options['httpauth'] = CURLAUTH_NTLM;
  1267. break;
  1268. case "anysafe":
  1269. $curl_options['httpauth'] = CURLAUTH_ANYSAFE;
  1270. break;
  1271. case "any":
  1272. default:
  1273. $curl_options['httpauth'] = CURLAUTH_ANY;
  1274. }
  1275. curl_setopt($ch, CURLOPT_HTTPAUTH, $curl_options['httpauth']);
  1276. }
  1277. $remotedata = curl_exec($ch);
  1278. if ($curl_options['debug']) {
  1279. $status['cURL']['data'][]= $remotedata;
  1280. $status['debug'][]='CURL_INFO'.': '.print_r(curl_getinfo($ch), true);
  1281. }
  1282. if (curl_error($ch)) {
  1283. $status['error'][] = JFusionCurl::_('CURL_ERROR_MSG').": ".curl_error($ch);
  1284. curl_close($ch);
  1285. return $status;
  1286. }
  1287. curl_close($ch);
  1288. //we have to delete the cookies now
  1289. $status=JFusionCurl::deletemycookies($status, $cookies_to_set, $curl_options['cookiedomain'], $curl_options['cookiepath'], $curl_options['leavealone'], $curl_options['secure'], $curl_options['httponly'], $curl_options['crossdomain_url']);
  1290. $cookies_to_set_index = 0;
  1291. return $status;
  1292. }
  1293. /**
  1294. * remote logout url
  1295. *
  1296. * @param array $curl_options curl options
  1297. *
  1298. * @return string something
  1299. */
  1300. function RemoteLogoutUrl($curl_options)
  1301. {
  1302. $status=array();
  1303. global $ch;
  1304. global $cookiearr;
  1305. global $cookies_to_set;
  1306. global $cookies_to_set_index;
  1307. $tmpurl = array();
  1308. $cookies_to_set=array();
  1309. $cookies_to_set_index=0;
  1310. $status['debug']=array();
  1311. $status['error']=array();
  1312. $status['cURL']=array();
  1313. $status['cURL']['moodle']='';
  1314. $status['cURL']['data']= array();
  1315. $open_basedir = ini_get('open_basedir');
  1316. $safe_mode = ini_get('safe_mode');
  1317. // check parameters and set defaults
  1318. if (!isset($curl_options['post_url'])) {
  1319. $status['error'][]= 'Fatal programming error : no post_url!';
  1320. return $status;
  1321. }
  1322. if (!isset($curl_options['cookiedomain'])) {
  1323. $curl_options['cookiedomain'] = '';
  1324. }
  1325. if (!isset($curl_options['cookiepath'])) {
  1326. $curl_options['cookiepath'] = '';
  1327. }
  1328. if (!isset($curl_options['leavealone'])) {
  1329. $curl_options['leavealone'] = null;
  1330. }
  1331. if (!isset($curl_options['secure'])) {
  1332. $curl_options['secure'] = 0;
  1333. }
  1334. if (!isset($curl_options['httponly'])) {
  1335. $curl_options['httponly'] = 0;
  1336. }
  1337. if (!isset($curl_options['verifyhost'])) {
  1338. $curl_options['verifyhost'] = 1;
  1339. }
  1340. if (!isset($curl_options['crossdomain_url'])) {
  1341. $curl_options['crossdomain_url'] = '';
  1342. }
  1343. if (!isset($curl_options['debug'])) {
  1344. $curl_options['debug'] = false;
  1345. }
  1346. if (!isset($curl_options['postfields'])) {
  1347. $curl_options['postfields'] = "";
  1348. }
  1349. $ch = curl_init();
  1350. curl_seto

Large files files are truncated, but you can click here to view the full file