PageRenderTime 54ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/src/site/tmp/install_4a925da139185/admin/models/model.curl.php

https://bitbucket.org/manchas/jrobotz
PHP | 858 lines | 688 code | 63 blank | 107 comment | 129 complexity | 49db9b59fa485cde4bf0fba80a032919 MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-2.1, GPL-2.0, Apache-2.0
  1. <?php
  2. /**
  3. * @package JFusion
  4. * @subpackage Models
  5. * @author JFusion development team -- Henk Wevers
  6. * @copyright Copyright (C) 2008 JFusion -- Henk Wevers. All rights reserved.
  7. * @license http://www.gnu.org/copyleft/gpl.html GNU/GPL
  8. */
  9. // no direct access
  10. defined('_JEXEC' ) or die('Restricted access' );
  11. /**
  12. * load the JFusion framework
  13. */
  14. require_once(JPATH_ADMINISTRATOR .DS.'components'.DS.'com_jfusion'.DS.'models'.DS.'model.jfusion.php');
  15. /**
  16. * HTML Form Parser
  17. * This will extract all forms and his elements in an
  18. * big assoc Array.
  19. *
  20. * @package JFusion
  21. * @version $Id 1.0 -- mod HJW-4
  22. * @author Peter Valicek <Sonny2@gmx.DE>
  23. * @copyright 2004 Peter Valicek Peter Valicek <Sonny2@gmx.DE>: GPL-2
  24. *
  25. *
  26. * Many modifications and bug repairs by Henk Wevers
  27. */
  28. class JFusionCurlHtmlFormParser {
  29. var $html_data = '';
  30. var $_return = array();
  31. var $_counter = '';
  32. var $button_counter = '';
  33. var $_unique_id = '';
  34. function JFusionCurlHtmlFormParser( $html_data ) {
  35. if ( is_array($html_data) ) {
  36. $this->html_data = join('', $html_data);
  37. } else {
  38. $this->html_data = $html_data;
  39. }
  40. $this->_return = array();
  41. $this->_counter = 0;
  42. $this->button_counter = 0;
  43. $this->_unique_id = md5(time());
  44. }
  45. function parseForms() {
  46. if ( preg_match_all("/<form.*>.+<\/form>/isU", $this->html_data, $forms) ) {
  47. foreach ( $forms[0] as $form ) {
  48. $this->button_counter = 0;
  49. #form details
  50. preg_match("/<form.*?name=[\"']?([\w\s-]*)[\"']?[\s>]/i", $form, $form_name);
  51. if ($form_name) {$this->_return[$this->_counter]['form_data']['name'] = preg_replace("/[\"'<>]/", "", $form_name[1]);}
  52. preg_match("/<form.*?action=\"(.*?)\"|'(.*?)'?[\s]>/is", $form, $action);
  53. if ($action) {$this->_return[$this->_counter]['form_data']['action'] = preg_replace("/[\"'<>]/", "", $action[1]);}
  54. preg_match("/<form.*?method=[\"']?([\w\s]*)[\"']?[\s>]/i", $form, $method);
  55. if ($method) {$this->_return[$this->_counter]['form_data']['method'] = preg_replace("/[\"'<>]/", "", $method[1]);}
  56. preg_match("/<form.*?enctype=(\"([^\"]*)\"|'([^']*)'|[^>\s]*)([^>]*)?>/i", $form, $enctype);
  57. if ($enctype) {$this->_return[$this->_counter]['form_data']['enctype'] = preg_replace("/[\"'<>]/", "", $enctype[1]);}
  58. preg_match("/<form.*?id=[\"']?([\w\s-]*)[\"']?[\s>]/i", $form, $id);
  59. if ($id) {$this->_return[$this->_counter]['form_data']['id'] = preg_replace("/[\"'<>]/", "", $id[1]);}
  60. # form elements: input type = hidden
  61. if ( preg_match_all("/<input[^<>]+type=[\"']hidden[\"'][^<>]+>/iU", $form, $hiddens) ) {
  62. foreach ( $hiddens[0] as $hidden ) {
  63. $this->_return[$this->_counter]['form_elements'][$this->_getName($hidden)] =
  64. array(
  65. 'type' => 'hidden',
  66. 'value'=> $this->_getValue($hidden)
  67. );
  68. }
  69. }
  70. # form elements: input type = text
  71. if ( preg_match_all("/<input[^<>]+type=[\"']text[\"'][^<>]+>/iU", $form, $texts) ) {
  72. foreach ( $texts[0] as $text ) {
  73. $this->_return[$this->_counter]['form_elements'][$this->_getName($text)] =
  74. array(
  75. 'type' => 'text',
  76. 'value' => $this->_getValue($text),
  77. 'id' => $this->_getId($text),
  78. 'class' => $this->_getClass($text)
  79. );
  80. }
  81. }
  82. # form elements: input type = password
  83. if ( preg_match_all("/<input[^<>]+type=[\"']password[\"'][^<>]+>/iU", $form, $passwords) ) {
  84. foreach ( $passwords[0] as $password ) {
  85. $this->_return[$this->_counter]['form_elements'][$this->_getName($password)] =
  86. array(
  87. 'type' => 'password',
  88. 'value' => $this->_getValue($password)
  89. );
  90. }
  91. }
  92. # form elements: textarea
  93. if ( preg_match_all("/<textarea.*>.*<\/textarea>/isU", $form, $textareas) ) {
  94. foreach ( $textareas[0] as $textarea ) {
  95. preg_match("/<textarea.*>(.*)<\/textarea>/isU", $textarea, $textarea_value);
  96. $this->_return[$this->_counter]['form_elements'][$this->_getName($textarea)] =
  97. array(
  98. 'type' => 'textarea',
  99. 'value' => $textarea_value[1]
  100. );
  101. }
  102. }
  103. # form elements: input type = checkbox
  104. if ( preg_match_all("/<input[^<>]+type=[\"']checkbox[\"'][^<>]+>/iU", $form, $checkboxes) ) {
  105. foreach ( $checkboxes[0] as $checkbox ) {
  106. if ( preg_match("/checked/i", $checkbox) ) {
  107. $this->_return[$this->_counter]['form_elements'][$this->_getName($checkbox)] =
  108. array(
  109. 'type' => 'checkbox',
  110. 'value' => 'on'
  111. );
  112. } else {
  113. $this->_return[$this->_counter]['form_elements'][$this->_getName($checkbox)] =
  114. array(
  115. 'type' => 'checkbox',
  116. 'value' => ''
  117. );
  118. }
  119. }
  120. }
  121. # form elements: input type = radio
  122. if ( preg_match_all("/<input[^<>]+type=[\"']radio[\"'][^<>]+>/iU", $form, $radios) ) {
  123. foreach ( $radios[0] as $radio ) {
  124. if ( preg_match("/checked/i", $radio) ) {
  125. $this->_return[$this->_counter]['form_elements'][$this->_getName($radio)] =
  126. array(
  127. 'type' => 'radio',
  128. 'value' => $this->_getValue($radio)
  129. );
  130. }
  131. }
  132. }
  133. # form elements: input type = submit
  134. if ( preg_match_all("/<input[^<>]+type=[\"']submit[\"'][^<>]+>/iU", $form, $submits) ) {
  135. foreach ( $submits[0] as $submit ) {
  136. $this->_return[$this->_counter]['buttons'][$this->button_counter] =
  137. array(
  138. 'type' => 'submit',
  139. 'name' => $this->_getName($submit),
  140. 'value' => $this->_getValue($submit)
  141. );
  142. $this->button_counter++;
  143. }
  144. }
  145. # form elements: input type = button
  146. if ( preg_match_all("/<input[^<>]+type=[\"']button[\"'][^<>]+>/iU", $form, $buttons) ) {
  147. foreach ( $buttons[0] as $button ) {
  148. $this->_return[$this->_counter]['buttons'][$this->button_counter] =
  149. array(
  150. 'type' => 'button',
  151. 'name' => $this->_getName($button),
  152. 'value' => $this->_getValue($button)
  153. );
  154. $this->button_counter++;
  155. }
  156. }
  157. # form elements: input type = reset
  158. if ( preg_match_all("/<input[^<>]+type=[\"']reset[\"'][^<>]+>/iU", $form, $resets) ) {
  159. foreach ( $resets[0] as $reset ) {
  160. $this->_return[$this->_counter]['buttons'][$this->button_counter] =
  161. array(
  162. 'type' => 'reset',
  163. 'name' => $this->_getName($reset),
  164. 'value' => $this->_getValue($reset)
  165. );
  166. $this->button_counter++;
  167. }
  168. }
  169. # form elements: input type = image
  170. if ( preg_match_all("/<input[^<>]+type=[\"']image[\"'][^<>]+>/iU", $form, $images) ) {
  171. foreach ( $images[0] as $image ) {
  172. $this->_return[$this->_counter]['buttons'][$this->button_counter] =
  173. array(
  174. 'type' => 'image',
  175. 'name' => $this->_getName($image),
  176. 'value' => $this->_getValue($image)
  177. );
  178. $this->button_counter++;
  179. }
  180. }
  181. # input type=select entries
  182. # Here I have to go on step around to grep at first all select names and then
  183. # the content. Seems not to work in an other way
  184. if ( preg_match_all("/<select.*>.+<\/select>/isU", $form, $selects) ) {
  185. foreach ( $selects[0] as $select ) {
  186. if ( preg_match_all("/<option.*>.+<\/option>/isU", $select, $all_options) ) {
  187. foreach ( $all_options[0] as $option ) {
  188. if ( preg_match("/selected/i", $option) ) {
  189. if ( preg_match("/value=[\"'](.*)[\"']\s/iU", $option, $option_value) ) {
  190. $option_value = $option_value[1];
  191. $found_selected = 1;
  192. } else {
  193. preg_match("/<option.*>(.*)<\/option>/isU", $option, $option_value);
  194. $option_value = $option_value[1];
  195. $found_selected = 1;
  196. }
  197. }
  198. }
  199. if ( !isset($found_selected) ) {
  200. if ( preg_match("/value=[\"'](.*)[\"']/iU", $all_options[0][0], $option_value) ) {
  201. $option_value = $option_value[1];
  202. } else {
  203. preg_match("/<option>(.*)<\/option>/iU", $all_options[0][0], $option_value);
  204. $option_value = $option_value[1];
  205. }
  206. } else {
  207. unset($found_selected);
  208. }
  209. $this->_return[$this->_counter]['form_elements'][$this->_getName($select)] =
  210. array(
  211. 'type' => 'select',
  212. 'value' => trim($option_value)
  213. );
  214. }
  215. }
  216. }
  217. # Update the form counter if we have more then 1 form in the HTML table
  218. $this->_counter++;
  219. }
  220. }
  221. return $this->_return;
  222. }
  223. function _getName( $string ) {
  224. if (preg_match("/name=(\"([^\"]*)\"|'([^']*)'|[^>\s]*)([^>]*)?>/is", $string, $match) ) {
  225. #preg_match("/name=[\"']?([\w\s]*)[\"']?[\s>]/i", $string, $match) ) { -- did not work as expected
  226. $val_match = trim($match[1]);
  227. $val_match = trim($val_match,"\"\'");
  228. unset($string);
  229. return trim($val_match,'"');
  230. }
  231. }
  232. function _getValue( $string ) {
  233. if ( preg_match("/value=(\"([^\"]*)\"|'([^']*)'|[^>\s]*)([^>]*)?>/is", $string, $match) ) {
  234. $val_match = trim($match[1]);
  235. $val_match = trim($val_match,"\"\'");
  236. unset($string);
  237. return $val_match;
  238. }
  239. }
  240. function _getId( $string ) {
  241. if (preg_match("/id=(\"([^\"]*)\"|'([^']*)'|[^>\s]*)([^>]*)?>/is", $string, $match) ) {
  242. #preg_match("/name=[\"']?([\w\s]*)[\"']?[\s>]/i", $string, $match) ) { -- did not work as expected
  243. $val_match = trim($match[1]);
  244. $val_match = trim($val_match,"\"\'");
  245. unset($string);
  246. return $val_match;
  247. }
  248. }
  249. function _getClass( $string ) {
  250. if (preg_match("/class=(\"([^\"]*)\"|'([^']*)'|[^>\s]*)([^>]*)?>/is", $string, $match) ) {
  251. $val_match = trim($match[1]);
  252. $val_match = trim($val_match,"\"\'");
  253. unset($string);
  254. return $val_match;
  255. }
  256. }
  257. }
  258. /**
  259. * Singleton static only class that creates instances for each specific JFusion plugin.
  260. * @package JFusion
  261. */
  262. class JFusionCurl{
  263. /*
  264. * function read_header
  265. * Basic code was found on Svetlozar Petrovs website http://svetlozar.net/page/free-code.html.
  266. * The code is free to use and similar code can be found on other places on the net.
  267. */
  268. function read_header($ch, $string){
  269. global $location;
  270. global $cookiearr;
  271. global $ch;
  272. global $cookies_to_set;
  273. global $cookies_to_set_index;
  274. $length = strlen($string);
  275. if(!strncmp($string, "Location:", 9)){
  276. $location = trim(substr($string, 9, -1));
  277. }
  278. if(!strncmp($string, "Set-Cookie:", 11)){
  279. header($string,false);
  280. $cookiestr = trim(substr($string, 11, -1));
  281. $cookie = explode(';', $cookiestr);
  282. $cookies_to_set[$cookies_to_set_index] = $cookie;
  283. $cookies_to_set_index++;
  284. $cookie = explode('=', $cookie[0]);
  285. $cookiename = trim(array_shift($cookie));
  286. $cookiearr[$cookiename] = trim(implode('=', $cookie));
  287. }
  288. $cookie = "";
  289. if(trim($string) == ""){
  290. foreach ($cookiearr as $key=>$value){
  291. $cookie .= "$key=$value ";
  292. }
  293. curl_setopt($ch, CURLOPT_COOKIE, $cookie);
  294. }
  295. return $length;
  296. }
  297. /*
  298. * function parseURL
  299. * out[0] = full url
  300. * out[1] = scheme or '' if no scheme was found
  301. * out[2] = username or '' if no auth username was found
  302. * out[3] = password or '' if no auth password was found
  303. * out[4] = domain name or '' if no domain name was found
  304. * out[5] = port number or '' if no port number was found
  305. * out[6] = path or '' if no path was found
  306. * out[7] = query or '' if no query was found
  307. * out[8] = fragment or '' if no fragment was found
  308. */
  309. function parseUrl ($url){
  310. $r = '!(?:(\w+)://)?(?:(\w+)\:(\w+)@)?([^/:]+)?';
  311. $r .= '(?:\:(\d*))?([^#?]+)?(?:\?([^#]+))?(?:#(.+$))?!i';
  312. preg_match ( $r, $url, $out );
  313. return $out;
  314. }
  315. function parsecookies($cookielines){
  316. $line=array();
  317. $cookies=array();
  318. foreach ($cookielines as $line){
  319. $cdata = array();
  320. $data = array();
  321. foreach( $line as $data ) {
  322. $cinfo = explode( '=', $data );
  323. $cinfo[0] = trim( $cinfo[0] );
  324. if (!isset($cinfo[1])) {$cinfo[1]='';}
  325. if (strcasecmp($cinfo[0],'expires')== 0) $cinfo[1] = strtotime( $cinfo[1]);
  326. if (strcasecmp($cinfo[0],'secure')== 0) $cinfo[1] = "true";
  327. if (strcasecmp($cinfo[0],'httponly')== 0) $cinfo[1] = "true";
  328. if (in_array( strtolower($cinfo[0]), array( 'domain', 'expires', 'path', 'secure', 'comment', 'httponly'))) {
  329. $cdata[trim( $cinfo[0] )] = $cinfo[1];
  330. }
  331. else {
  332. $cdata['value']['key'] = $cinfo[0];
  333. $cdata['value']['value'] = $cinfo[1];
  334. }
  335. }
  336. $cookies[] = $cdata;
  337. }
  338. return $cookies;
  339. }
  340. function setmycookies($status,$mycookies_to_set,$cookiedomain,$cookiepath,$expires=0,$secure=0,$httponly=1){
  341. $cookies=array();
  342. $cookies=JFusionCurl::parsecookies($mycookies_to_set);
  343. foreach ($cookies as $cookie){
  344. global $hnd;
  345. $name="";
  346. $value="";
  347. if ($expires == 0) {
  348. $expires_time=0;
  349. }
  350. else {
  351. $expires_time=time()+$expires;
  352. }
  353. if (isset($cookie['value']['key'])) {$name= $cookie['value']['key'];}
  354. if (isset($cookie['value']['value'])) {$value=$cookie['value']['value'];}
  355. if (isset($cookie['expires'])) {$expires_time=$cookie['expires'];}
  356. if (!$cookiepath) {if (isset($cookie['path'])) {$cookiepath=$cookie['path'];}}
  357. if (!$cookiedomain){if (isset($cookie['domain'])) {$cookiedomain=$cookie['domain'];}}
  358. setcookie($name, urldecode($value),$expires_time,$cookiepath,$cookiedomain,$secure,$httponly);
  359. if ( ($expires_time) == 0) {$expires_time='Session_cookie';}
  360. else {$expires_time=date('d-m-Y H:i:s',$expires_time);}
  361. $status['debug'][] = JText::_('CREATED') . ' ' . JText::_('COOKIE') . ': ' . JText::_('NAME') . '=' . $name . ', ' . JText::_('VALUE') . '=' . urldecode($value) .', ' .JText::_('EXPIRES') . '=' .$expires_time .', ' . JText::_('COOKIE_PATH') . '=' . $cookiepath . ', ' . JText::_('COOKIE_DOMAIN') . '=' . $cookiedomain. ', '.JText::_('COOKIE_SECURE') . '=' .$secure. ', '.JText::_('COOKIE_HTTPONLY') . '=' .$httponly;
  362. if($name=='MOODLEID_'){
  363. $status['cURL']['moodle'] = urldecode($value);
  364. }
  365. }
  366. return $status;
  367. }
  368. function deletemycookies($status,$mycookies_to_set,$cookiedomain,$cookiepath,$leavealone,$secure=0,$httponly=1){
  369. $cookies=array();
  370. $cookies=JFusionCurl::parsecookies($mycookies_to_set);
  371. // leavealone keys/values while deleting
  372. // the $leavealone is an array of key=value that controls cookiedeletion
  373. // key = value
  374. // if key is an existing cookiename then that cookie will be affected depending on the value
  375. // if value = '>' then the 'name' cookies with an expiration date/time > now() will not be deleted
  376. // if value = '0' then the 'name' cookies will never be deleted at all
  377. // if name is a string than the cookie with that name will be affected
  378. // if name = '0' then all cookies will be affected according to the value
  379. // thus
  380. // MOODLEID_=> keeps the cookie with the name MOODLEID_ if expirationtime lies after now()
  381. // 0=> will keep all cookies that are not sessioncookies
  382. // 0=0 will keep all cookies
  383. if ($leavealone){
  384. $leavealonearr = array();
  385. $lines = array();
  386. $line=array();
  387. $lines = explode(',',$leavealone);
  388. $i = 0;
  389. foreach ($lines as $line) {
  390. $cinfo = explode ('=',$line);
  391. $leavealonearr[$i]['name'] = $cinfo[0];
  392. $leavealonearr[$i]['value'] = $cinfo[1];
  393. $i++;
  394. }
  395. }
  396. foreach ($cookies as $cookie){
  397. // check if we schould leave the cookie alone
  398. $leaveit = false;
  399. if ($leavealone){
  400. for ($i=0;$i<count($leavealonearr);$i++){
  401. if (isset($cookie['value']['key'])){
  402. if (($cookie['value']['key']== $leavealonearr[$i]['name']) ||
  403. ($leavealonearr[$i]['name']=='0')){
  404. if (($leavealonearr[$i]['value'] == '0')||($cookie['expires'] > time())){
  405. $leaveit = true;
  406. }
  407. }
  408. }
  409. }
  410. }
  411. $name="";
  412. $value="";
  413. $expires_time=time()-30*60;
  414. if (isset($cookie['value']['key'])) {$name= $cookie['value']['key'];}
  415. if (isset($cookie['expires'])) {$expires_time=$cookie['expires'];}
  416. if (!$cookiepath) {if (isset($cookie['path'])) {$cookiepath=$cookie['path'];}}
  417. if (!$cookiedomain){if (isset($cookie['domain'])) {$cookiedomain=$cookie['domain'];}}
  418. if($name=='MOODLEID_'){
  419. $status['cURL']['moodle'] = urldecode($cookie['value']['value']);
  420. }
  421. if (!$leaveit){
  422. setcookie($name, urldecode($value),$expires_time,$cookiepath,$cookiedomain,$secure,$httponly);
  423. if ( ($expires_time) == 0) {$expires_time='Session_cookie';}
  424. else {$expires_time=date('d-m-Y H:i:s',$expires_time);}
  425. $status['debug'][] = JText::_('DELETED') . ' ' . JText::_('COOKIE') . ': ' . JText::_('NAME') . '=' . $name . ', ' . JText::_('VALUE') . '=' . urldecode($value) .', ' .JText::_('EXPIRES') . '=' .$expires_time .', ' . JText::_('COOKIE_PATH') . '=' . $cookiepath . ', ' . JText::_('COOKIE_DOMAIN') . '=' . $cookiedomain. ', '.JText::_('COOKIE_SECURE') . '=' .$secure. ', '.JText::_('COOKIE_HTTPONLY') . '=' .$httponly;
  426. } else{
  427. setcookie($name, urldecode($cookie['value']['value']),$expires_time,$cookiepath,$cookiedomain,$secure,$httponly);
  428. if ( ($expires_time) == 0) {$expires_time='Session_cookie';}
  429. else {$expires_time=date('d-m-Y H:i:s',$expires_time);}
  430. $status['debug'][] = JText::_('LEFT_ALONE') . ' ' . JText::_('COOKIE') . ': ' . JText::_('NAME') . '=' . $name . ', ' . JText::_('VALUE') . '=' . urldecode($cookie['value']['value']) .', ' .JText::_('EXPIRES') . '=' .$expires_time .', ' . JText::_('COOKIE_PATH') . '=' . $cookiepath . ', ' . JText::_('COOKIE_DOMAIN') . '=' . $cookiedomain. ', '.JText::_('COOKIE_SECURE') . '=' .$secure. ', '.JText::_('COOKIE_HTTPONLY') . '=' .$httponly;
  431. }
  432. }
  433. return $status;
  434. }
  435. /*
  436. * function RemoteLogin
  437. * Smart function to programatically login to an JFusion integration
  438. * Will determine what to post (including, optionally, hidden form inputs) and what cookies to set.
  439. * Will then login.
  440. * In addition to username and password the function only needs an URL to a page with a loginform
  441. * and the ID of the loginform.
  442. * Including button information and hidden input posts is optionally
  443. */
  444. function RemoteLogin($curl_options){
  445. global $ch;
  446. global $cookiearr;
  447. global $cookies_to_set;
  448. global $cookies_to_set_index;
  449. $status = array();
  450. $tmpurl = array();
  451. $overridearr = array();
  452. $newhidden = array();
  453. $lines = array();
  454. $line=array();
  455. $cookies_to_set=array();
  456. $status['debug']=array();
  457. $status['error']=array();
  458. $status['cURL']=array();
  459. $status['cURL']['moodle']='';
  460. $cookies_to_set_index=0;
  461. // check if curl extension is loaded
  462. if (!extension_loaded('curl')) {
  463. $status['error'][] = JText::_('CURL_NOTINSTALLED');
  464. return $status;
  465. }
  466. // check parameters and set defaults
  467. if (!isset($curl_options['post_url']) || !isset($curl_options['formid']) ||
  468. !isset($curl_options['username']) || !isset($curl_options['password'])){
  469. $status['error'][] = JText::_('CURL_FATAL');
  470. return $status;
  471. }
  472. if (!isset($curl_options['integrationtype'])) {$curl_options['integrationtype'] = 1;}
  473. if (!isset($curl_options['relpath'])) {$curl_options['relpath'] = false;}
  474. if (!isset($curl_options['hidden'])) {$curl_options['hidden'] = false;}
  475. if (!isset($curl_options['buttons'])) {$curl_options['buttons'] = false;}
  476. if (!isset($curl_options['override'])) {$curl_options['override'] = NULL;}
  477. if (!isset($curl_options['cookiedomain'])) {$curl_options['cookiedomain'] = '';}
  478. if (!isset($curl_options['cookiepath'])) {$curl_options['cookiepath'] = '';}
  479. if (!isset($curl_options['expires'])) {$curl_options['expires'] = 1800;}
  480. if (!isset($curl_options['input_username_id'])) {$curl_options['input_username_id'] = '';}
  481. if (!isset($curl_options['input_password_id'])) {$curl_options['input_password_id'] = '';}
  482. if (!isset($curl_options['secure'])) {$curl_options['secure'] = 0;}
  483. if (!isset($curl_options['httponly'])) {$curl_options['httponly'] = 0;}
  484. if (!isset($curl_options['verifyhost'])) {$curl_options['verifyhost'] = 1;}
  485. // find out if we have a SSL enabled website
  486. if (strpos( $curl_options['post_url'],'https://') === false){
  487. $ssl_string = 'http://';
  488. } else {
  489. $ssl_string = 'https://';
  490. }
  491. # prevent usererror by not supplying trailing backslash
  492. # if (!(substr($curl_options['post_url'],-1) == "/")) {
  493. # $curl_options['post_url'] = $curl_options['post_url']."/";
  494. # }
  495. $status['debug'][] = JText::_('CURL_POST_URL_1')." ".$curl_options['post_url'];
  496. # read the login page
  497. $ch = curl_init();
  498. curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
  499. curl_setopt($ch, CURLOPT_URL,$curl_options['post_url']);
  500. curl_setopt($ch, CURLOPT_REFERER, "");
  501. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
  502. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST,$curl_options['verifyhost']);
  503. curl_setopt($ch, CURLOPT_FAILONERROR,1);
  504. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  505. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);
  506. curl_setopt($ch, CURLOPT_HEADERFUNCTION, array('JFusionCurl','read_header'));
  507. $remotedata = curl_exec($ch);
  508. if (curl_error($ch)) {
  509. $status['error'][] = JText::_('CURL_ERROR_MSG').": ".curl_error($ch);
  510. curl_close($ch);
  511. return $status;
  512. }
  513. if ($curl_options['integrationtype'] ==1){
  514. curl_close($ch);
  515. }
  516. $cookies_to_set_index=0;
  517. $status['debug'][] = JText::_('CURL_PHASE_1');
  518. $status=JFusionCurl::setmycookies($status,$cookies_to_set,$curl_options['cookiedomain'],$curl_options['cookiepath'],$curl_options['expires'],$curl_options['secure'],$curl_options['httponly']);
  519. #find out if we have the form with the name/id specified
  520. $parser = new JFusionCurlHtmlFormParser( $remotedata);
  521. $result = $parser->parseForms();
  522. $frmcount = count($result);
  523. $myfrm = -1;
  524. $i = 0;
  525. do {
  526. if (isset($result[$i]['form_data']['name'])){
  527. if ($result[$i]['form_data']['name']==$curl_options['formid']){
  528. $myfrm = $i;
  529. break;
  530. }
  531. }
  532. if (isset($result[$i]['form_data']['id'])){
  533. if ($result[$i]['form_data']['id']==$curl_options['formid']){
  534. $myfrm = $i;
  535. break;
  536. }
  537. }
  538. $i +=1;
  539. } while ($i<$frmcount);
  540. if ($myfrm == -1) {
  541. $helpthem = '';
  542. if ($frmcount >0) {
  543. $i = 0;
  544. $helpthem = 'I found';
  545. do {
  546. if (isset($result[$i]['form_data']['id'])){
  547. $helpthem = $helpthem.' -- Name='.$result[$i]['form_data']['name'].' &ID='.$result[$i]['form_data']['id'];
  548. }
  549. $i +=1;
  550. } while ($i<$frmcount);
  551. }
  552. $status['error'][] = JText::_('CURL_NO_LOGINFORM')." ".$helpthem;
  553. return $status;
  554. }
  555. $status['debug'][] = JText::_('CURL_VALID_FORM');
  556. // by now we have the specified login form, lets get the data needed to login
  557. // we went to all this trouble to get to the hidden input entries.
  558. // The stuff is there to enhance security and is, yes, hidden
  559. $form_action= $result[$myfrm]['form_data']['action'];
  560. $form_method= $result[$myfrm]['form_data']['method'];
  561. $elements_keys = array_keys($result[$myfrm]['form_elements']);
  562. $elements_values= array_values($result[$myfrm]['form_elements']);
  563. $elements_count = count($result[$myfrm]['form_elements']);
  564. // override keys/values from hidden inputs
  565. // the $override is an array of keys/values that override existing keys/values
  566. if ($curl_options['override']){
  567. $lines = explode(',',$curl_options['override']);
  568. foreach ($lines as $line) {
  569. $cinfo = explode ('=',$line);
  570. $overridearr[$cinfo[0]]['value'] = $cinfo[1];
  571. $overridearr[$cinfo[0]]['type'] = 'hidden';
  572. }
  573. $newhidden= array_merge($result[$myfrm]['form_elements'],$overridearr);
  574. $elements_keys = array_keys($newhidden);
  575. $elements_values= array_values($newhidden);
  576. $elements_count = count($newhidden);
  577. }
  578. // now construct the action parameter
  579. // we have 4 possible options:
  580. // case 0 Form action is without httpo.. and relpath = 0 , special case
  581. // case 1 Form action is without http.. and relpath = 1 , just construct the action
  582. // case 2 form_action is a full url, eg http..... and relpath = 0 This is easy, we do nothing at all
  583. // case 3 form_action is a full url, eg http..... and relpath = 1 special case
  584. $rel = (int)($curl_options['relpath']);
  585. if (substr($form_action,0,strlen($ssl_string))== $ssl_string) $hashttp = 2; else $hashttp = 0;
  586. switch($rel+$hashttp) {
  587. case 0:
  588. #add a / in front of form_action
  589. if (substr($form_action,0,1) != "/") {
  590. $form_action = '/'.$form_action;
  591. }
  592. # we need to correct various situations like
  593. # relative url from basedir, relative url from postdir etc
  594. $tmpurl = JFusionCurl::parseUrl($curl_options['post_url']);
  595. $pathinfo1 = pathinfo($form_action);
  596. $pathinfo = pathinfo($tmpurl[6]);
  597. //$status['debug'][] = 'post_url : '.print_r($curl_options['post_url'],true);
  598. //$status['debug'][] = 'tmpurl : '.print_r($tmpurl,true);
  599. //$status['debug'][] = 'form_action: '.print_r($form_action,true);
  600. //$status['debug'][] = 'pathinfo1 : '.print_r($pathinfo1,true);
  601. //$status['debug'][] = 'pathinfo : '.print_r($pathinfo,true);
  602. if ($pathinfo[dirname] == $pathinfo1[dirname]){$pathinfo[dirname]='';} #prevent double directory
  603. $form_action = $ssl_string.$tmpurl[4].$pathinfo[dirname].$form_action;
  604. //$status['debug'][] = 'form_action_final: '.print_r($form_action,true);
  605. break;
  606. case 1:
  607. #add a / in front of form_action
  608. if (substr($form_action,0,1) != "/") {
  609. $form_action = '/'.$form_action;
  610. }
  611. $curl_options['post_url']=rtrim($curl_options['post_url'],'/');
  612. $form_action = $curl_options['post_url'].$form_action;
  613. break;
  614. case 2:
  615. //do nothing at all
  616. break;
  617. case 3:
  618. // reserved, maybe something pops up, then we use this
  619. break;
  620. }
  621. $input_username_name="";
  622. for ($i = 0; $i <= $elements_count-1; $i++) {
  623. if ($curl_options['input_username_id']) {
  624. if (strtolower($elements_keys[$i]) == strtolower($curl_options['input_username_id'])){
  625. $input_username_name=$elements_keys[$i];
  626. break;
  627. }
  628. }
  629. if ($input_username_name == ""){
  630. if (strpos(strtolower($elements_keys[$i]),'user')!==false){
  631. $input_username_name=$elements_keys[$i];
  632. }
  633. if (strpos(strtolower($elements_keys[$i]),'name')!==false){
  634. $input_username_name=$elements_keys[$i];
  635. }
  636. }
  637. }
  638. if ($input_username_name==""){
  639. $status['error'][] = JText::_('CURL_NO_NAMEFIELD');
  640. return $status;
  641. }
  642. $input_password_name="";
  643. for ($i = 0; $i <= $elements_count-1; $i++) {
  644. if ($curl_options['input_password_id']) {
  645. if (strtolower($elements_keys[$i]) == strtolower($curl_options['input_password_id'])){
  646. $input_password_name=$elements_keys[$i];
  647. break;
  648. }
  649. }
  650. if (strpos(strtolower($elements_keys[$i]),'pass')!==false){
  651. $input_password_name=$elements_keys[$i];
  652. }
  653. }
  654. if ($input_password_name==""){
  655. $status['error'][] = JText::_('CURL_NO_PASSWORDFIELD');
  656. return $status;
  657. }
  658. $status['debug'][] = JText::_('CURL_VALID_USERNAME');
  659. // we now set the submit parameters. These are:
  660. // all form_elements name=value combinations with value != '' and type hidden
  661. $strParameters="";
  662. if ($curl_options['hidden']) {
  663. for ($i = 0; $i <= $elements_count-1; $i++) {
  664. if (($elements_values[$i] ['value'] != '')&& ($elements_values[$i] ['type'] == 'hidden')){
  665. $strParameters .= '&'.$elements_keys[$i].'='.urlencode($elements_values[$i] ['value']);
  666. }
  667. }
  668. }
  669. /* // have to figure out how to handle buttons exactly, seems not important anyway. leave it for now
  670. if ($curl_options['buttons']){
  671. if (isset($result[$myfrm] ['buttons'][0]['type'])) {
  672. if ($result[$myfrm] ['buttons'][0]['type'] =='submit'){
  673. $strParameters .= '&'.'submit'.'='.urlencode($result[$myfrm] ['buttons'][0]['value']);
  674. }
  675. }
  676. }
  677. */
  678. // code for buttons submitted by Daniel Baur
  679. if ($curl_options['buttons']){
  680. if (isset($result[$myfrm] ['buttons'][0]['type'])) {
  681. if ($result[$myfrm] ['buttons'][0]['type'] =='submit'){
  682. if ($result[$myfrm]['buttons'][0]['name']){
  683. $strParameters .= '&'.$result[$myfrm]['buttons'][0]['name'].'='.urlencode($result[$myfrm] ['buttons'][0]['value']);
  684. }
  685. else{
  686. $strParameters .= '&'.'submit'.'='.urlencode($result[$myfrm] ['buttons'][0]['value']);
  687. }
  688. }
  689. }
  690. }
  691. $post_params = $input_username_name."=".urlencode($curl_options['username'])."&".$input_password_name."=".urlencode($curl_options['password']);
  692. $post_params_debug = $input_username_name."=".urlencode($curl_options['username'])."&".$input_password_name."=xxxxxx";
  693. $status['debug'][] = JText::_('CURL_STARTING_LOGIN')." ".$form_action." parameters= ".$post_params_debug.$strParameters;
  694. // finally submit the login form:
  695. if ($curl_options['integrationtype'] == 1){
  696. $ch = curl_init();
  697. curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
  698. curl_setopt($ch, CURLOPT_REFERER, "");
  699. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
  700. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST,$curl_options['verifyhost']);
  701. curl_setopt($ch, CURLOPT_FAILONERROR,1);
  702. curl_setopt($ch, CURLOPT_HEADERFUNCTION, array('JFusionCurl','read_header'));
  703. }
  704. curl_setopt($ch, CURLOPT_URL,$form_action);
  705. curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
  706. curl_setopt($ch, CURLOPT_POST, 1);
  707. curl_setopt($ch, CURLOPT_POSTFIELDS, $post_params.$strParameters);
  708. $remotedata = curl_exec($ch);
  709. if (curl_error($ch)) {
  710. $status['error'][] = JText::_('CURL_ERROR_MSG').": ".curl_error($ch);
  711. curl_close($ch);
  712. return $status;
  713. }
  714. curl_close($ch);
  715. #we have to set the cookies now
  716. $status['debug'][] = JText::_('CURL_LOGIN_FINISHED');
  717. $status=JFusionCurl::setmycookies($status,$cookies_to_set,$curl_options['cookiedomain'],$curl_options['cookiepath'],$curl_options['expires'],$curl_options['secure'],$curl_options['httponly']);
  718. $cookies_to_set_index=0;
  719. return $status;
  720. }
  721. /*
  722. * @function RemoteLogout
  723. * @returns $status
  724. */
  725. function RemoteLogout($curl_options) {
  726. $status=array();
  727. global $ch;
  728. global $cookiearr;
  729. global $cookies_to_set;
  730. global $cookies_to_set_index;
  731. $tmpurl = array();
  732. $cookies_to_set=array();
  733. $cookies_to_set_index=0;
  734. $status['debug']=array();
  735. $status['error']=array();
  736. $status['cURL']=array();
  737. $status['cURL']['moodle']='';
  738. // check parameters and set defaults
  739. if (!isset($curl_options['post_url'])){
  740. $status['error'][]= 'Fatal programming error : no post_url!';
  741. return $status;
  742. }
  743. if (!isset($curl_options['cookiedomain'])) {$curl_options['cookiedomain'] = '';}
  744. if (!isset($curl_options['cookiepath'])) {$curl_options['cookiepath'] = '';}
  745. if (!isset($curl_options['leavealone'])) {$curl_options['leavealone'] = NULL;}
  746. if (!isset($curl_options['secure'])) {$curl_options['secure'] = 0;}
  747. if (!isset($curl_options['httponly'])) {$curl_options['httponly'] = 0;}
  748. if (!isset($curl_options['verifyhost'])) {$curl_options['verifyhost'] = 1;}
  749. # prevent usererror by not supplying trailing backslash
  750. if (!(substr($curl_options['post_url'],-1) == "/")) {
  751. $curl_options['post_url'] = $curl_options['post_url']."/";
  752. }
  753. $ch = curl_init();
  754. curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
  755. curl_setopt($ch, CURLOPT_REFERER, "");
  756. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
  757. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST,$curl_options['verifyhost']);
  758. curl_setopt($ch, CURLOPT_FAILONERROR,1);
  759. curl_setopt($ch, CURLOPT_HEADERFUNCTION, array('JFusionCurl','read_header'));
  760. curl_setopt($ch, CURLOPT_URL,$curl_options['post_url']);
  761. curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
  762. $remotedata = curl_exec($ch);
  763. if (curl_error($ch)) {
  764. $status['error'][] = JText::_('CURL_ERROR_MSG').": ".curl_error($ch);
  765. curl_close($ch);
  766. return $status;
  767. }
  768. curl_close($ch);
  769. #we have to delete the cookies now
  770. $status=JFusionCurl::deletemycookies($status,$cookies_to_set,$curl_options['cookiedomain'],$curl_options['cookiepath'],$curl_options['leavealone'],$curl_options['secure'],$curl_options['httponly']);
  771. $cookies_to_set_index=0;
  772. return $status;
  773. }
  774. }
  775. ?>