|
|
|
|
|
|
| |
This class is used to add data to existing MySpace.com user profile using PHP cURL. User is authenticated with their login information and data - in form of plain text or HTML code - is saved with the existing profile data. User selects the section of their profile where the data is to be added by this class.
Version 1.0, Created 04/09/2006
Author - Ehsan Haque
Web - http://ehsan.bdwebwork.com/
Bug Fixed & Updated by - MA Razzaque Rupom
Web - http://www.rupom.info
process.php
| <?php
if ((empty($_POST['email'])) || (empty($_POST['password'])) || (empty($_POST['data'])))
{
header("Location: form.html");
return false;
}
require_once("class.MySpace.php");
$email = $_POST['email'];
$password = $_POST['password'];
$section = $_POST['section'];
$data = $_POST['data'];
$myspace = new MySpace;
/*
Set to true if HTTP proxy is required to browse net
- Setting it to true will require to provide Proxy Host Name and Port number
*/
$myspace->isUsingProxy = false;
// Set the Proxy Host Name
$myspace->proxyHost = "";
// Set the Proxy Port Number
$myspace->proxyPort = "";
// Set the location to save the Cookie Jar File
$myspace->cookieJarPath = $_SERVER['DOCUMENT_ROOT'] . "/MySpace/";
/*
Execute the Service
- Require to pass MySpace.com Account Username and Password
*/
$execute = $myspace->execService($email, $password, $section, $data);
if (!empty($myspace->errorInfo))
{
echo $myspace->errorInfo;
return false;
}
$message = $myspace->getStatusMessage();
echo "<script language='javascript'>";
echo "alert('" . $message . "')\n";
echo "location.href='form.html'";
echo "</script>";
?> | |
class.MySpace.php
| <?php
/*--------------------------------------------------------------------------
About the class - class.MySpace.php
This class is used to add data to existing MySpace.com user profile using
PHP cURL. User is authenticated with their login information and data
- in form of plain text or HTML code - is saved with the existing profile
data. User selects the section of their profile where the data is to be
added by this class.
Version 1.0, Created 04/09/2006
Author - Ehsan Haque
Web - http://ehsan.bdwebwork.com/
Bug Fixed & Updated by - MA Razzaque Rupom
Web - http://www.rupom.info
License: GPL
--------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------
Public Variables - Alphabetical List
+ cookieJarPath ---------------- Absolute path to write Cookie file
+ data ---------------- Data to be added
+ email ---------------- Email for Login
+ isUsingProxy ---------------- Specifies if the server is using proxy
+ password ---------------- Password for Login
+ proxyHost ---------------- Host address for proxy
+ proxyPort ---------------- Port number for proxy
+ section ---------------- Section to add data
--------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------
Private Variables - Alphabetical List
+ addDataURI ---------- URL to be used to post data
+ allData ---------- Existing profile data from MySpace.com
+ cookie ---------- Set to 1 (true) if cookie is to be used
in header
+ cookieFile ---------- Temporary cookie file name
+ cookieFileJar ---------- Temporary file where cookie is saved
+ follow ---------- Set to 1 (true) if header redirection(s)
are to be performed
+ getDataURI ---------- URL to retrieve existing data
+ loginURI ---------- URL for login to MySpace.com
+ postFields ---------- Set the fields to be sent via POST as name=value
+ proxy ---------- Proxy server address as host:port
+ referer ---------- Referer URL used by cURL
+ returnTransfer ---------- Set to 1 (true) if the output is to
saved in as a string
+ sectionFieldName ---------- Form field name for selected section
+ showHeader ---------- Set to 1 (true) if header information
is to be printed in the output
+ statusMsg ---------- Success or Failure message
+ url ---------- URL to be used by cURL
+ userAgent ---------- User agent used by cURL
+ usePostField ---------- Set to 1 (true) if any data is
sent via POST
--------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------
Functions - Alphabetical List
--------------------------------------------------------------------------*/
class MySpace
{
/*--------------------------------------------------------------------------
Public Variables
--------------------------------------------------------------------------*/
/**
* MySpace.com Email address for login
* @public
* @var string
*/
var $email = "";
/**
* MySpace.com Password for login
* @public
* @var string
*/
var $password = "";
/**
* MySpace.com Section for data addition
* @public
* @var string
*/
var $section = "";
/**
* Data that is to be added to MySpace.com selected Section
* @public
* @var string
*/
var $data = "";
/**
* Abosolute path to save the cookie
* Default value is DOCUMENT_ROOT
* @public
* @var string
*/
var $cookieJarPath = "";
/**
* Specifies if Proxy server required as Gateaway
* Default value is false
* @public
* @var boolean
*/
var $isUsingProxy = false;
/**
* Proxy host name
* @public
* @var string
*/
var $proxyHost = "";
/**
* Proxy port number
* @public
* @var int
*/
var $proxyPort = 0;
/*--------------------------------------------------------------------------
Private Variables
--------------------------------------------------------------------------*/
/**
* URL to Authenticate user on MySpace.com
* @private
* @var string
*/
var $loginURI = "http://login.myspace.com/index.cfm?fuseaction=login.process&MyToken=f806380a-c55b-42d1-8535-9508c24d50c5";
/**
* URL to Authenticate user on MySpace.com
* @private
* @var string
*/
var $addDataURI = "http://profileedit.myspace.com/Modules/ProfileEdit/Pages/Interests.aspx?fuseaction=profile.interests&MyToken=f9420a7d-1a88-46c7-8dee-5765c85d15ba";
/**
* URL to Authenticate user on MySpace.com
* @private
* @var string
*/
var $getDataURI = "http://profileedit.myspace.com/Modules/ProfileEdit/Pages/Interests.aspx?fuseaction=profile.interests&MyToken=f9420a7d-1a88-46c7-8dee-5765c85d15ba";
/**
* URL to be used by cURL
* @private
* @var string
*/
var $url = "";
/**
* User agent (used to trick Yahoo!)
* @private
* @var string
*/
var $userAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.0.6) Gecko/20060728 Firefox/1.5.0.6";
/**
* Referer URL (used to trick Yahoo!)
* @private
* @var string
*/
var $referer = "http://www.myspace.com";
/**
* Specifies whether output includes the header
* @private
* @var int
*/
var $showHeader = 0;
/**
* Specifies if cURL should follow the redirected URL
* @private
* @var int
*/
var $follow = 0;
/**
* Specifies if cURL should save the output to a string
* @private
* @var int
*/
var $returnTransfer = 1;
/**
* Specifies number of post fields to pass
* @private
* @var int
*/
var $usePostField = 0;
/**
* Specify fields to send via POST method as key=value
* @private
* @var string
*/
var $postFields = "";
/**
* All profile data from MySpace.com
* @private
* @var array
*/
var $allData = array();
/**
* Specify field name for the selected Section
* @private
* @var string
*/
var $sectionFieldName = "";
/**
* File where Cookie is temporarily saved
* @private
* @var string
*/
var $cookieFileJar = "";
/**
* Cookie File that is read by service process
* This carries same value as $cookieFileJar
* @private
* @var string
*/
var $cookieFile = "";
/**
* Specifies if Cookie is to be in header
* @private
* @var int
*/
var $cookie = 0;
/**
* Proxy address as proxy.host:port
* @private
* @var string
*/
var $proxy = "";
/**
* Status message as found from the profile update page
* @private
* @var string
*/
var $statusMsg = "";
/*--------------------------------------------------------------------------
Function Definitions
--------------------------------------------------------------------------*/
/**
* Executes the Service
* @param string $login Username of user's MySpace.com Account
* @param string $password Password of the user's MySpace.com Account
* @param string $section Section where user's information will be added
* @param string $data Data that is to be added to the selected section
* @return array|false
*/
function execService($email, $password, $section, $data)
{
$email = trim($email);
$password = trim($password);
if (empty($email))
{
$this->setError("provide_email");
return false;
}
if (empty($password))
{
$this->setError("provide_pass");
return false;
}
if (empty($section))
{
$this->setError("select_section");
return false;
}
if (empty($data))
{
$this->setError("empty_data");
return false;
}
$this->email = $email;
$this->password = $password;
$this->section = $section;
$this->data = $data;
$this->setSectionFieldName();
// Instructs to authenticate user on MySpace.com
$this->auth = $this->doAuthentication();
if ($this->auth)
{
// Adds data to the selected secion
$this->doAddData();
}
// Unlinks the cookie file
$this->unlinkFile($this->cookieFileJar);
}
/**
* Sets Service URL
* @return void
*/
function setSectionFieldName()
{
if (empty($this->section))
{
$this->setError("select_section");
return false;
}
$this->sectionFieldName = 'ctl00$ctl00$Main$ProfileEditContent$editInterests$';
// Sets the URL depending on the choosen service
switch ($this->section)
{
case 'aboutme' : $this->sectionFieldName .= "AboutMeText"; break;
case 'liketomeet' : $this->sectionFieldName .= "LikeToMeetText"; break;
case 'general' : $this->sectionFieldName .= "GeneralText"; break;
case 'music' : $this->sectionFieldName .= "MusicText"; break;
case 'movies' : $this->sectionFieldName .= "MoviesText"; break;
case 'television' : $this->sectionFieldName .= "TelevisionText"; break;
case 'books' : $this->sectionFieldName .= "BooksText"; break;
case 'heroes' : $this->sectionFieldName .= "HeroesText"; break;
}
}
/**
* Authenticates user on MySpace.com
* @return boolean
*/
function doAuthentication()
{
// Instructs to initialize cURL session
$this->initCurl();
// Sets the URL for authentication purpose
$this->url = $this->loginURI;
// Sets the number of fields to send via HTTP POST
$this->usePostField = 1;
// Sets the fields to be sent via HTTP POST as key=value
$this->postFields = "email=$this->email&password=$this->password&ctl00$Main$SplashDisplay$login$loginbutton=&Remember=";
$this->follow = 1;
// Instructs to set Cookie Jar
$this->setCookieJar();
// Checks if the cURL options are all set properly
if ($this->setCurlOption())
{
// Instructs to execute cURL session
$this->execCurl();
// Checks if any cURL error is generated
if ($this->getCurlError())
{
$this->unlinkFile($this->cookieFileJar);
$this->setError("curl_error");
return false;
}
// Checks if the authentication failed, either invalid login or username is not registered
if (preg_match("/Must Be Logged-In/i", $this->outputContent))
{
// Instructs to close cURL session
$this->closeCurl();
// Unlinks the cookie file
$this->unlinkFile($this->cookieFileJar);
$this->setError("invalid_login");
return false;
}
$this->closeCurl();
}
unset($this->outputContent);
return true;
}
/**
* Adds the profile data on MySpace.com
* @return array|false
*/
function doAddData()
{
$this->getAllData();
if (empty($this->allData))
{
$this->setError("profile_data_empty");
return false;
}
$this->initCurl();
$this->url = $this->addDataURI;
$this->referer = "http://profileedit.myspace.com/index.cfm?fuseaction=profile.interests&MyToken=a0ef3ff6-5b8c-4d7b-9fdb-38c2e5df01f5";
$this->usePostField = 1;
$this->header = 0;
foreach ($this->allData as $key => $value)
{
$addValue = ($key == $this->sectionFieldName) ? stripslashes($this->data).$value : $value;
$addValue = urlencode($addValue);
$postFields[] = "$key=$addValue";
}
$this->postFields = implode("&", $postFields);
$this->cookieFile = $this->cookieFileJar;
$this->follow = 0;
if ($this->setCurlOption())
{
$this->execCurl();
// Pattern to match the confirmation message
$pattern = "/profile\supdated/i";
preg_match($pattern, $this->outputContent, $match);
if (!empty($match))
{
$this->setStatusMessage(true);
}
else
{
$this->setStatusMessage(false);
}
$this->closeCurl();
return true;
}
return false;
}
/**
* Gets the profile data from MySpace.com
* @return array|false
*/
function getAllData()
{
$this->initCurl();
$this->url = $this->getDataURI;
$this->referer = "http://profileedit.myspace.com/index.cfm?fuseaction=profile.interests&MyToken=6f4851ca-a9bc-43de-bde7-4991ec305c0c";
$this->header = 0;
$this->cookieFile = $this->cookieFileJar;
$this->follow = 0;
if ($this->setCurlOption())
{
$this->execCurl();
// Searching for HIDDEN INPUT tags
$patternHiddens = "/\<input+.+hidden+.+>/iU";
preg_match_all($patternHiddens, $this->outputContent, $matchHiddens);
if (!empty($matchHiddens[0]))
{
// Processing HIDDEN INPUT tags as name = value
foreach ($matchHiddens[0] as $key => $value)
{
$patterForName = "/name=\"\S*\"/iU";
preg_match($patterForName, $value, $matchName);
$refinedName = str_replace("\"", "", str_replace("name=", "", $matchName[0]));
$patterForValue = "/value=\"\S*\"/iU";
preg_match($patterForValue, $value, $matchValue);
$refinedValue = str_replace("\"", "", str_replace("value=", "", $matchValue[0]));
if ((preg_match("/VIEWSTATE/i", $refinedName)) || (preg_match("/hash/iU", $refinedName)))
{
$result[$refinedName] = $refinedValue;
}
}
}
// Searching for SUBMIT INPUT tags
$patternSubmits = "/\<input+.+submit+.+>/iU";
preg_match_all($patternSubmits, $this->outputContent, $matchSubmits);
if (!empty($matchSubmits[0]))
{
// Processing SUBMIT INPUT tags as name = value
foreach ($matchSubmits[0] as $key => $value)
{
$patterForName = "/name=\"\S*\"/iU";
preg_match($patterForName, $value, $matchName);
$refinedName = str_replace("\"", "", str_replace("name=", "", $matchName[0]));
$patterForValue = "/value=\".*?\"/iU";
preg_match($patterForValue, $value, $matchValue);
$refinedValue = str_replace("\"", "", str_replace("value=", "", $matchValue[0]));
if (preg_match("/save/iU", $refinedValue))
{
$result[$refinedName] = $refinedValue;
}
}
}
// Searching for TEXTAREA tags
$this->outputContent = preg_replace("/\<br>\<\/textarea>/iU","",$this->outputContent);
$patternTAs = "/\<textarea.+>.*\<\/textarea>/iU";
preg_match_all($patternTAs, $this->outputContent, $matchTAs);
if (!empty($matchTAs[0]))
{
// Processing TEXTAREA tags as name = value
foreach ($matchTAs[0] as $key => $value)
{
$patterForName = "/name=\"\S*\"/iU";
preg_match($patterForName, $value, $matchName);
$refinedName = str_replace("\"", "", str_replace("name=", "", $matchName[0]));
$patterForValues = "/\<textarea.+>.*\<\/textarea>/iU";
preg_match($patterForValues, $value, $matchValue);
$replacePattern = array (
'/<textarea.+\">/iU',
'/<\/textarea>/iU'
);
$replaceText = array (
'',
''
);
$refinedValue = preg_replace($replacePattern, $replaceText, $matchValue[0]);
$result[$refinedName] = $refinedValue;
}
}
$this->closeCurl();
$this->allData = $result;
return true;
}
return false;
}
/**
* Initializes cURL session
* @return void
*/
function initCurl()
{
$this->curlSession = curl_init();
}
/**
* Sets the Cookie Jar File where Cookie is temporarily saved
* @return void
*/
function setCookieJar()
{
// Sets the encrypted cookie filename using MySpace.com account username
$this->cookieFilename = MD5($this->email);
// Sets the Cookie Jar filename with an absolute path
$this->cookieFileJar = (!empty($this->cookieJarPath)) ? $this->cookieJarPath . "/" . $this->cookieFilename : $_SERVER['DOCUMENT_ROOT'] . "/" . $this->cookieFilename;
fopen($this->cookieFileJar, "w");
}
/**
* Sets cURL options
* @return boolean
*/
function setCurlOption()
{
// Sets the User Agent
curl_setopt($this->curlSession, CURLOPT_USERAGENT, $this->userAgent);
// Sets the HTTP Referer
curl_setopt($this->curlSession, CURLOPT_REFERER, $this->referer);
// Sets the URL that PHP will fetch using cURL
curl_setopt($this->curlSession, CURLOPT_URL, $this->url);
// Sets the number of fields to be passed via HTTP POST
curl_setopt($this->curlSession, CURLOPT_POST, $this->usePostField);
// Sets the fields to be sent via HTTP POST as key=value
curl_setopt($this->curlSession, CURLOPT_POSTFIELDS, $this->postFields);
// Sets the filename where cookie information will be saved
curl_setopt($this->curlSession, CURLOPT_COOKIEJAR, $this->cookieFileJar);
// Sets the filename where cookie information will be looked up
curl_setopt($this->curlSession, CURLOPT_COOKIEFILE, $this->cookieFile);
curl_setopt($this->curlSession, CURLOPT_HEADER, $this->header);
// Sets the option to set Cookie into HTTP header
curl_setopt($this->curlSession, CURLOPT_COOKIE, $this->cookie);
//curl_setopt($this->curlSession, CURLOPT_SSL_VERIFYPEER,false);
// Checks if the user needs proxy (to be set by user)
if ($this->isUsingProxy)
{
// Checks if the proxy host and port is specified
if ((empty($this->proxyHost)) || (empty($this->proxyPort)))
{
$this->setError("proxy_required");
$this->unlinkFile($this->cookieFileJar);
return false;
}
// Sets the proxy address as proxy.host:port
$this->proxy = $this->proxyHost . ":" . $this->proxyPort;
}
// Sets the proxy server as proxy.host:port
curl_setopt($this->curlSession, CURLOPT_PROXY, $this->proxy);
// Specifies whether to save the output to a string
curl_setopt($this->curlSession, CURLOPT_RETURNTRANSFER, $this->returnTransfer);
curl_setopt($this->curlSession, CURLOPT_CAINFO, $this->caBundleFile);
// Specifies whether to use all header redirections
curl_setopt($this->curlSession, CURLOPT_FOLLOWLOCATION, $this->follow);
return true;
}
/**
* Executes cURL Session
* @return void
*/
function execCurl()
{
$this->outputContent = curl_exec($this->curlSession);
}
/**
* Closes cURL session
* @return void
*/
function closeCurl()
{
curl_close($this->curlSession);
unset($this->curlSession);
}
/**
* Sets the either success/failure message
* @return void
*/
function setStatusMessage($status = null)
{
$this->statusMsg = ($status) ? "Profile updated successfully" : "Profile was not updated";
}
/**
* Returns the status message
* @return string
*/
function getStatusMessage()
{
return $this->statusMsg;
}
/**
* Unlinks any given file
* @return void
*/
function unlinkFile($fileToUnlink)
{
if (file_exists($fileToUnlink))
{
unlink($fileToUnlink);
}
}
/**
* Sets any cURL error generated
* @return boolean
*/
function getCurlError()
{
$this->curlError = curl_error($this->curlSession);
return (!empty($this->curlError)) ? true : false;
}
/**
* Sets Error Information
* @return void
*/
function setError($error)
{
$msg = (!empty($error)) ? $this->getErrorInfo($error) : null;
$this->errorCount++;
$this->errorInfo = $msg;
}
/**
* Provides the Error message
* @param string $error Error code for which error message is generated
* @return string
*/
function getErrorInfo($error)
{
switch ($error)
{
case 'provide_service' : $msg = "Must specify a Service"; break;
case 'provide_email' : $msg = "Must provide Email address"; break;
case 'provide_pass' : $msg = "Must provide Password"; break;
case 'select_section' : $msg = "Must select Section"; break;
case 'profile_data_empty' : $msg = "Could not retrieve Profile Data"; break;
case 'emp |
| |