Skip to content

Instantly share code, notes, and snippets.

@mikevalstar
Created June 28, 2011 22:42
Show Gist options
  • Save mikevalstar/1052440 to your computer and use it in GitHub Desktop.
Save mikevalstar/1052440 to your computer and use it in GitHub Desktop.
domgrabber
<?PHP
function update_basecamp_img($filename){
if($this->basecamp_login != "" && $this->basecamp_password != ""){
//echo "Updating basecamp image";
$S = new Snoopy();
/* Login the user */
$login_url = "https://" . BasecampSiteURL . '/login';
$S->fetch($login_url);
$S->setcookies();
$dom = new domDocument;
$dom->preserveWhiteSpace = false;
$dom->loadHTML($S->results);
$inputs = $dom->getElementsByTagName('input');
$token = "";
for($x = 0; $x < $inputs->length; $x++)
if($inputs->item($x)->getAttribute('name') == 'authenticity_token')
$token = $inputs->item($x)->getAttribute('value');
$vars = array();
$vars["authenticity_token"] = $token;
$vars["username"] = $this->basecamp_login;
$vars["password"] = $this->basecamp_password;
$vars['commit'] = "Sign in";
$vars['product'] = "basecamp";
$vars['remember_me'] = "1";
$vars['subdomain'] = BasecampSubdomain;
//echo "Submitting";
$auth_url = "https://launchpad.37signals.com/authenticate";
$S->submit($auth_url, $vars);//print_r($S->cookies);
$S->referer = $login_url;
$dom = new domDocument;
$dom->preserveWhiteSpace = false;
@$dom->loadHTML($S->results); // error supressed for this page
$ele = $dom->getElementById('settings_signout_and_help');
if($ele){
//echo "User Authenticated";
}else{
//echo $S->results;
//print_r( $S->headers );
trigger_error("Unable to auth user", E_USER_NOTICE);
return;
}
/* change the user's info */
$identity_url = "https://" . BasecampSiteURL . '/identity/edit';
$S->referer = '';
$S->fetch($identity_url);
$dom = new domDocument;
$dom->preserveWhiteSpace = false;
@$dom->loadHTML($S->results);//echo $S->results;
$inputs = $dom->getElementsByTagName('input');
$vars = array();
for($x = 0; $x < $inputs->length; $x++)
if($inputs->item($x)->getAttribute('name') != 'signal_id_identity[avatar]')
$vars[$inputs->item($x)->getAttribute('name')] = $inputs->item($x)->getAttribute('value');
$vars['signal_id_identity[locale]'] = "en";
$forms = $dom->getElementsByTagName('form');
for($x = 0; $x < $forms->length; $x++)
$update_form = $forms->item($x)->getAttribute('action');
$files = array("signal_id_identity[avatar]" => '@' . UPLOAD_DIR . 'orig/' . $filename );//print_r($vars);
$vars["signal_id_identity[avatar]"] = '@' . UPLOAD_DIR . 'orig/' . $filename;
$S->referer = $identity_url;//echo "https://" . BasecampSiteURL . $update_form;
//$S->set_submit_multipart();
//$S->submit("https://" . BasecampSiteURL . $update_form, $vars, $files); echo $S->results;
//$file_to_upload = array('file_contents'=>'@'.$file_name_with_full_path);
$cookie_str = '';
foreach ( $S->cookies as $cookieKey => $cookieVal )
$cookie_str .= $cookieKey."=".urlencode($cookieVal)."; ";
//print_r($vars);
$ch = curl_init();
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_7; en-US) AppleWebKit/534.16 (KHTML, like Gecko) Chrome/10.0.648.204 Safari/534.16");
curl_setopt($ch, CURLOPT_COOKIE, $cookie_str);
curl_setopt($ch, CURLOPT_REFERER, $identity_url);
curl_setopt($ch, CURLOPT_URL,"https://" . BasecampSiteURL . $update_form);
curl_setopt($ch, CURLOPT_POST,1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $vars);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result=curl_exec ($ch);
//print_r(curl_getinfo($ch)); print_r( $result ); echo curl_error($ch); echo "https://" . BasecampSiteURL . $update_form;
curl_close ($ch);
//die("\n\n\n\n\nMade it to end");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment