(Currently pre-release) If you distribute or use this, please leave this message intact. */ define('DALIB_STATUS_LOGGEDIN', 1); define('DALIB_STATUS_LOGGEDOUT', 2); define('DALIB_REUSETOKEN', 0x1); class DALib { var $username = null; var $authtoken = null; var $cookie = ''; var $status = DALIB_STATUS_LOGGEDOUT; function login($username, $password, $flags = DALIB_REUSETOKEN){ $postargs = array('username' => $username, 'password' => $password); if($flags & DALIB_REUSETOKEN){ $postargs['reusetoken'] = 1; } $http = new DALib_HTTPRequest($this); if($http->post('http://www.deviantart.com/users/login', $postargs)){ if(preg_match("/Set-Cookie: userinfo=(.*);/Ui", $http->headers, $matches)){ $this->cookie = "Cookie: userinfo={$matches[1]};\n"; $userinfo = unserialize(urldecode($matches[1])); if($userinfo['username'] && $userinfo['authtoken']){ $this->username = $userinfo['username']; $this->authtoken = $userinfo['authtoken']; $this->status = DALIB_STATUS_LOGGEDIN; return true; }else{ return false; } }else{ return false; } } } function loggedin(){ if($this->status == DALIB_STATUS_LOGGEDIN){ return true; }else{ return false; } } } class DALib_HTTPRequest{ var $page; var $headers; var $da; function DALib_HTTPRequest($da){ if($da){ $this->da = $da; }else{ die("DALib_HTTPRequest needs a reference to a DALib object"); } } function post($url, $postargs){ if(is_array($postargs)){ $postdata = array(); foreach($postargs as $name => $value){ $postdata[] = urlencode($name).'='.urlencode($value); } $postdata = implode('&', $postdata); }else{ $postdata = $postargs; } if(preg_match("/^http:\/\/(.+)(?::([\d]+))?(\/.*)$/Ui", $url, $matches)){ $host = $matches[1]; $port = ($matches[2] != '') ? $matches[2] : 80; $path = $matches[3]; $request = "POST $path HTTP/1.0\r\n". "Host: $host\r\n". "User-Agent: DALib/0.1\r\n". ($this->da->cookie ? $this->da->cookie : ''). "Content-Type: application/x-www-form-urlencoded\r\n". "Content-Length: ".strlen($postdata)."\r\n\r\n". "$postdata\r\n\r\n"; if($sock = fsockopen($host, $port)){ fwrite($sock, $request); while(!feof($sock)){ $response .= fgets($sock, 4096); } fclose($sock); list($this->headers, $this->page) = explode("\n\n", $response, 2); return true; }else{ return false; } }else{ return false; } } } define("DEV_CRIT_ENCOURAGED", 64); define("DEV_CRIT_WELCOME", 0); define("DEV_CRIT_DISCOURAGED", 32); define("DEV_CRIT_COMMENTS_DISABLED", 16); class DALib_NewDeviation{ var $posturl = "http://www.deviantart.com/submit/step4-writing"; var $devpoetry = ''; var $devpreview = ''; var $devcatid = 1123; var $devtitle = false; var $devdesc = false; var $devkeywords = false; var $devisadult = ''; var $devismobile = ''; var $devislit = ''; var $devcritpref = DEV_CRIT_WELCOME; function DALib_NewDeviation($da){ if($da){ $this->da = $da; }else{ die("DALib_NewDeviation needs a reference to a DALib object"); } } function post(){ if(in_array(false, array($this->devtitle, $this->devdesc, $this->devkeywords))){ echo "ERROR: At least one of the required fields for submitting a new deviation was left blank"; return false; }else{ $http = new DALib_HTTPRequest($this->da); $postargs = array( "devpoetry" => $this->devpoetry, "devpreiew" => $this->devpreview, "devcatid" => $this->devcatid, "devtitle" => $this->devtitle, "devdesc" => $this->devdesc, "devkeywords" => $this->devkeywords, "devisadult" => $this->devisadult, "devidmobile" => $this->devismobile, "devislit" => $this->devislit, "devcritpref" => $this->devcritpref, ); $http->post($this->posturl, $postargs); if(preg_match("/Click here<\/a> to view your newly submitted deviation/Ui", $http->page, $matches)){ return $matches[1]; }else{ echo "Submit failed! Error page below:\n\n". str_replace("\n", "
", htmlentities($http->page)); return false; } } } }