ak = $ak; $this->pk = $pk; $this->unconfirmedtoken = $unconfirmed; $this->confirmedtoken = $confirmed; /* CHECK IF WE ARE AUTHENTICATING */ if (isset($_GET['unconfirmedToken'])) { if (!$this->getNewConfirmedToken($_GET['unconfirmedToken'])) { $this->error = 'could not authenticate'; return false; } } } function readConfirmedToken($filename = 'token'){ if ($token_file = @fopen($filename,'r')) { $this->confirmedtoken = fread($token_file, 40); fclose($token_file); return true; } return false; } function getNewConfirmedToken($unconfirmedtoken){ $a = &new Auth($this); if ($token_file = @fopen('token','w')) { $this->confirmedtoken = $a->GetConfirmedToken($unconfirmedtoken); fwrite($token_file, $this->confirmedtoken); fclose($token_file); return true; } return false; } } /****************************************************** * OpenomyAPI : base class for all API classes ******************************************************/ class OpenomyAPI { var $server = "www.openomy.com"; var $apipath = "/api/rest/"; var $apiroot = ""; var $proxy = PROXY_ADDRESS; var $proxyport = PROXY_PORT; var $useproxy = PROXY_ENABLED; var $useragent = "openomy-php/0.1"; var $appkey = ""; var $privatekey = ""; var $havedata = false; var $debug = false; function OpenomyAPI() { $this->apiroot = $this->server . $this->apipath; } /* You should call this method to share your appkey and privkey between all api objects */ function setKeys($applicationkey, $privatekey) { $this->appkey = $applicationkey; $this->privatekey = $privatekey; $this->havedata = true; } function debug($v) { $this->debug = $v; } function initialize($at = null) { if ($at->ak && $at->pk) { $this->setKeys($at->ak, $at->pk); } if (!$this->havedata) { echo 'NO KEYS SET'; } } // generates the md5 hash of the query string arguments function gen_signature($params) { ksort($params); $res = ''; foreach ($params as $key => $val) { $res .= $key.'='.$val; } $res .= $this->privatekey; return md5($res); } // makes an array into a query string function gen_querystring($params) { $ret = array(); foreach ($params as $key => $val) { $ret[] = $key.'='.$val; } return implode('&',$ret); } // add all of the boring parameters that we all need function fill_params($params) { $params['applicationKey'] = $this->appkey; $params['signature'] = OpenomyAPI::gen_signature($params); return $params; } // send a get request to the server function get_request($params) { $uri = $this->apipath . "?" . $this->gen_querystring(OpenomyAPI::fill_params($params)); if ($this->useproxy) { $fp = fsockopen($this->proxy, $this->proxyport, $errno, $errstr, 5); $out = "GET http://".$this->server.$uri." HTTP/1.1\r\n"; $out .= "Host: ".$this->server."\r\n"; $out .= "User-Agent: ".$this->useragent."\r\n"; $out .= "Connection: Close\r\n\r\n"; fputs($fp, $out); } else { $fp = fopen('http://'.$this->server.$uri,'r'); } stream_set_timeout($fp, 180); $content = ''; $record = false; while (!feof($fp)) { $line = fgets($fp, 4096); if (strpos($line,"confirmedtoken; return parent::get_request($params); } // the authenticated version of the fill_params function. this one includes the confirmedToken function fill_params($params) { $params['confirmedToken'] = $this->confirmedtoken; return parent::fill_params($params); } } /****************************************************** * Tags: Implements all of the tag functions ******************************************************/ class Tags extends OpenomyAuthedAPI { function Tags(&$at){ $this->initialize($at); $this->confirmedtoken = $at->confirmedtoken; } function GetTag($tagid) { $params = array( "method" => "Tags.GetTag", "tagID" => $tagid); return OpenomyParser::parse_Tag(parent::get_request($params)); } function GetAllTags() { $params = array( "method" => "Tags.GetAllTags"); return OpenomyParser::parse_GetAllTags(parent::get_request($params)); } function CreateTag($tagname) { $params = array( "method" => "Tags.CreateTag", "tagName" => $tagname); return OpenomyParser::parse_NewTag(parent::get_request($params)); } function DeleteTag($tagId) { $params = array( "method" => "Tags.DeleteTag", "tagID" => $tagId); return OpenomyParser::check_error(parent::get_request($params)); } function AddFileToTag($fileId, $tagId) { $params = array( "method" => "Tags.AddFileToTag", "tagID" => $tagId, "fileID" => $fileId); return OpenomyParser::check_error(parent::get_request($params)); } function DeleteFileFromTag($fileId, $tagId) { $params = array( "method" => "Tags.DeleteFileFromTag", "tagID" => $tagId, "fileID" => $fileId); return OpenomyParser::check_error(parent::get_request($params)); } } /****************************************************** * Files: Implements all of the file functions ******************************************************/ class Files extends OpenomyAuthedAPI { var $downloadlink; var $at; function Files(&$at){ $this->at = &$at; $this->initialize($at); $this->confirmedtoken = $at->confirmedtoken; } function GetDownloadUrl($base, $filetoken) { return $base . "?" . OpenomyAPI::gen_querystring(parent::fill_params(array("fileToken" => $filetoken))); } function GetFile($fileid, $timeout = 5) { $params = array( "method" => "Files.GetFile", "fileID" => $fileid, "timeout" => $timeout ); $f = OpenomyParser::parse_GetFile(parent::get_request( $params )); return $f; } function AddFile($filename, $tmp_name, $tagid = 0) { $f = new FileUploader($this->at); $params = array( "tagID" => $tagid, "method" => "Files.AddFile" ); return OpenomyParser::check_error($f->upload_localfile($filename, $tmp_name, $params)); } function DeleteFile($fileid) { $params = array( "method" => "Files.DeleteFile", "fileID" => $fileid ); return OpenomyParser::check_error(parent::get_request($params)); } function ModifyFile($filename, $tmp_name, $fileid) { $f = new FileUploader($this->at); $params = array( "fileID" => $fileid, "method" => "Files.ModifyFile" ); return OpenomyParser::check_error($f->upload_localfile($filename, $tmp_name, $params)); } function GetAllFiles() { return OpenomyParser::parse_GetAllFiles(parent::get_request( array( "method" => "Files.GetAllFiles" ))); } } /****************************************************** * Util: Implements helper functions. these dont correspond to Api calls, but are helpful to have * around ******************************************************/ class Util extends OpenomyAPI { function Util(&$at){ $this->initialize($at); } function GetWebAuthUrl($params = array()) { $qs = OpenomyAPI::gen_querystring(parent::fill_params($params)); return "http://" . $this->server . "/api/login/?" . $qs; } function GetAuthUrl($uc) { $params = array(); $params["unconfirmedToken"] = $uc; $qs = OpenomyAPI::gen_querystring(parent::fill_params($params)); return "http://" . $this->server . "/api/login/?" . $qs; } } /****************************************************** * Auth: Implements all of the auth functions ******************************************************/ class Auth extends OpenomyAPI { function Auth(&$at){ $this->initialize($at); } function GetConfirmedToken($uc) { return OpenomyParser::parse_GetConfirmedToken($this->get_request( array( "method" => "Auth.GetConfirmedToken", "unconfirmedToken" => $uc ))); } function GetUnconfirmedToken() { return OpenomyParser::parse_GetUnconfirmedToken($this->get_request( array( "method" => "Auth.GetUnconfirmedToken" ))); } } /****************************************************** * FileUploader: Helper class for file uploads ******************************************************/ class FileUploader extends OpenomyAuthedAPI { function FileUploader(&$at){ $this->initialize($at); $this->confirmedtoken = $at->confirmedtoken; } function upload_localfile($filename, $tmp_name, $params = array()) { if ($fhandle = @fopen($tmp_name, 'r')) { $data = fread($fhandle, filesize($tmp_name)); return $this->upload($filename, $data, $params); } else { return array('error' => '0666', 'msg'=> 'no file'); } } function upload($remotefilename, $data, $params = array()) { // create a boundary $boundary = md5($remotefilename); // sort out the params $params = parent::fill_params($params); $qs = OpenomyAPI::gen_querystring($params); // prepare the content and that.. $query = $this->prepare_query($remotefilename, $params, $data, $boundary); if ($this->useproxy) { $fp = fsockopen($this->proxy, $this->proxyport, $errno, $errstr, 5); } else { $fp = fsockopen($this->server.$uri, 80, $errno, $errstr, 5); } /* CONSTRUCT THE HEADER */ $out = "POST http://".$this->server.$this->apipath." HTTP/1.1\r\n"; $out .= "User-Agent: ".$this->useragent."\r\n"; $out .= "Content-Type: ".$query[1]['Content-Type']."\r\n"; $out .= "Proxy-Connection: Close\r\n"; $out .= "Host: ".$this->server."\r\n"; $out .= "Content-Length: "; $out .= strlen($query[0])."\r\n\r\n"; /* ADD THE CONTENT */ $out .= $query[0]; // DO THE REQUEST fputs($fp, $out); stream_set_timeout($fp, 180); // read the response $content = ''; $record = false; while (!feof($fp)) { $line=fgets($fp, 512); if (strpos($line,"resolve_mime($filename); $ret = "content-disposition: form-data; name=\"fileField\"; filename=\"".$filename."\"\r\n"; $ret .= "Content-Type: ".$mimetype."\r\n\r\n" . $content . "\r\n"; return $ret; } function prepare_query($filename, $params, $data, $boundary) { $chunks = array(); foreach ($params as $k => $v) { $chunks[] = $this->to_multipart($k, $v); } $content = "--" . $boundary . "\r\n"; $content .= implode("--" . $boundary . "\r\n",$chunks); $content .= "--" . $boundary . "\r\n"; $content .= $this->file_to_multipart($filename, $data); $content .= "--" . $boundary . "--\r\n"; $header = array( "Content-Type" => "multipart/form-data; boundary=" . $boundary . " ", "User-Agent" => $this->useragent ); return array($content, $header); } } /****************************************************** * OpenomyParser: class to parse all returns from openomy * Mostly uses regexs rather than php's slow xml stuff ******************************************************/ class OpenomyParser { function OpenomyParser(){} function parseError($id, $msg, $ret = ''){ return array('error' => $id, 'msg' => trim($msg)); } // check for errors function check_error($txt) { $ret = preg_replace("/\r|\n/","",$txt); if (!preg_match("/.*<\/success>/", $ret)) { if (preg_match('/(.*)<\/error>/', $ret, $matches)) { $id = $matches[1]; $msg = $matches[2]; return OpenomyParser::parseError($id, $msg, $ret); } else { return OpenomyParser::parseError(0, "Unknown Api Error: ".$ret); } } return false; } // check for success function check_success($txt) { $ret = preg_replace("/\r|\n/","",$txt); if (preg_match("/.*<\/success>/", $ret)) { return true; } return false; } function parse_GetAllTags($txt) { if ($d = OpenomyParser::check_error($txt)) { return $d; } $d = preg_replace("/\r|\n/","",$txt); preg_match_all('/(.*)<\/tag>/', $txt, $matches); array_shift($matches); OpenomyParser::parse_MatchAll($matches, array('id', 'tag')); return $matches; } function parse_Tag($txt){ if ($d = OpenomyParser::check_error($txt)) { return $d; } $d = preg_replace("/\r|\n/","",$txt); preg_match('/(.*)<\/user>/', $txt, $users); array_shift($users); OpenomyParser::parse_MatchAll($users, array('user')); preg_match_all('/(.*)<\/file>/', $txt, $files); array_shift($files); OpenomyParser::parse_MatchAll($files, array('id','created','readaccess','writeaccess','file')); return array( 'id' => $id, 'name' => $name, 'created' => $created, 'users' => $users, 'files' => $files ); } function parse_NewTag($txt) { if ($d = OpenomyParser::check_error($txt)) { return $d; } $d = preg_replace("/\r|\n/","",$txt); preg_match('/(.*)<\/tag>/', $txt, $matches); return array( 'id' => $matches[1], 'name' => $matches[2] ); } function parse_GetFile($txt) { if ($d = OpenomyParser::check_error($txt)) { return $d; } $d = preg_replace("/\r|\n/","",$txt); preg_match('//', $txt, $id); preg_match('/(.*)<\/filename>/', $txt, $filename); preg_match('/(.*)<\/created>/', $txt, $created); preg_match('/(.*)<\/modified>/', $txt, $modified); preg_match('/(.*)<\/contenttype>/', $txt, $contenttype); preg_match('/(.*)<\/size>/', $txt, $size); preg_match('/(.*)<\/owner>/', $txt, $owner); preg_match('/(.*)<\/baseurl>/', $txt, $baseurl); preg_match('/(.*)<\/filetoken>/', $txt, $filetoken); preg_match('//', $txt, $ispublic); return array( 'filename' => $filename[1], 'created' => $created[1], 'modified' => $modified[1], 'contenttype' => $contenttype[1], 'sizeunits' => $size[1], 'size' => $size[2], 'owner' => $owner[1], 'baseurl' => $baseurl[1], 'filetoken' => $filetoken[1], 'ispublic' => $ispublic[1] ); } function parse_GetAllFiles($txt) { if ($d = OpenomyParser::check_error($txt)) { return $d; } $d = preg_replace("/\r|\n/","",$txt); preg_match_all('/(.*)<\/file>/', $txt, $matches); array_shift($matches); OpenomyParser::parse_MatchAll($matches, array('id', 'filename')); return $matches; } function parse_GetConfirmedToken($txt) { if ($d = OpenomyParser::check_error($txt)) { return $d; } $d = preg_replace("/\r|\n/","",$txt); if (preg_match("/(.*)<\/confirmedtoken>/", $txt, $matches)) { return $matches[1]; } } function parse_GetUnconfirmedToken($txt) { if ($d = OpenomyParser::check_error($txt)) { return $d; } $d = preg_replace("/\r|\n/","",$txt); if (preg_match("/(.*)<\/unconfirmedtoken>/", $txt, $matches)) { return $matches[1]; } } function parse_AddFile($txt) { if ($d = OpenomyParser::check_error($txt)) { return $d; } $d = preg_replace("/\r|\n/","",$txt); if (preg_match("/(.*)<\/fileid>/", $txt, $matches)) { return array('filedID' => $matches[1]); } } function parse_MatchAll(&$matches, $fields){ $newarray = array(); for ($x = 0; $x < sizeof($matches[0]); $x++) { $t = &$newarray[]; foreach ($fields as $key => $val) { $t[$val] = $matches[$key][$x]; } } $matches = $newarray; } } ?>