connect(); * ... do something... * $ws->disconnect(); * */ function _helper_recur(&$result, $node) { $name = $node->nodeName; $type = $node->nodeType; //$result[$name] = $type; if ( $node->hasChildNodes() ) { $children = $node->childNodes; for ( $i=0; $i<$children->length; $i++ ) { $subresult = array(); $item = $children->item($i); $nodename = $item->nodeName; if ( $item->nodeType == XML_TEXT_NODE || $item->nodeType == XML_CDATA_SECTION_NODE) { $result[] = $item->nodeValue; } else { _helper_recur($subresult, $item); if ( $item->hasAttributes() ) { $atts = $item->attributes; if ( !is_null($atts) ) { $subresult['@attributes'] = array(); foreach( $atts as $index=>$attr ) { $subresult['@attributes'][$attr->name] = $attr->value; } } } $result[$nodename][] = $subresult; } } } } function to_object($xmlstring) { $dom = new DOMDocument(); $dom->loadXML($xmlstring); $dom->normalizeDocument(); $result = array(); if ($dom->hasChildNodes() ) { $children = $dom->childNodes; for ( $i=0; $i<$children->length; $i++ ) { _helper_recur($result, $children->item($i) ); } } return $result; } class Waramu { public $statusPat = '/(.*?)<\/status>/'; public $uidPat = '/(.*?)<\/uid>/'; public $sessIdPat = '/(.*?)<\/sessionId>/'; private $c = NULL; public $sid = NULL; private $typeID = 4; private $_wloca = NULL; private $_wuser = NULL; private $_wpass = NULL; public function __construct() { $this->_wloca = WARAMU_WSDL; $this->_wuser = WARAMU_USER; $this->_wpass = WARAMU_PASS; $this->c = new SoapClient($this->_wloca); } /** * Returns 0 on success or error code from Waramu * */ public function connect() { $res = $this->newSession($this->_wuser, $this->_wpass); $stat = $this->_getStatus($res); if ( $stat == "0" ) { $this->sid = $this->_getSessionId($res); return 0; } else { return (int) $stat; } } public function disconnect() { $this->closeSession(); } public function setSid($sid) { $this->sid = $sid; } /******************************************************************** * WS methods * ********************************************************************/ /* * Session * */ public function newSession($uid, $pwd) { $res = $this->c->newSession(array('uid' => $uid, 'pwd' => $pwd)); return $res->return; } public function closeSession() { $res = $this->c->closeSession(array('sessId' => $this->sid)); } /* * descriptive methods * */ public function listTypes() { $p = array('sessId' => $this->sid); $res = $this->c->listTypes($p); $stat = $this->_getStatus($res->return); if ( $stat != "0" ) unset($_SESSION['waramu_session']); return to_object($res->return); } public function describeType($tid) { $p = array('sessId' => $this->sid, 'typeId' => $tid); $res = $this->c->describeType($p); $stat = $this->_getStatus($res->return); if ( $stat != "0" ) unset($_SESSION['waramu_session']); return to_object($res->return); } public function Identify() { $p = array('sessId' => $this->sid); $res = $this->c->Identify($p); return to_object($res->return); } /* * resource methods * @Returns int is case of error, or uid(String) with an uid * */ public function newResource($appUser, $metadata) { $typeId = "http://krihvel.opetaja.ee/schemas/Krihvel"; $typeName = "Krihvel"; $res = $this->c->newResource(array('appUser'=> $appUser, 'sessId' => $this->sid, 'data' => $metadata)); $stat = $this->_getStatus($res->return); if ( $stat == "0" ) { preg_match($this->uidPat, $res->return, $matches); return $matches[1]; } return (int) $stat; } public function updateResource($appUser, $uid, $metadata) { $params = array('appUser' => $appUser, 'sessId' => $this->sid, 'uid' => $uid, 'data' => $metadata); $res = $this->c->updateResource($params); return to_object($res->return); } public function setACL($appUser, $uid, $acl) { $params = array('appUser' => $appUser, 'sessId' => $this->sid, "uid" => $uid, "acl" => $acl); $res = $this->c->setACL($params); $stat = $this->_getStatus($res->return); return (int) $stat; } public function newPackage() { } public function getResource($au, $uid) { $params = array('sessId' => $this->sid, 'appUser' => $au, 'uid' => $uid); $res = $this->c->getResource($params); $stat = $this->_getStatus($res->return); if ( $stat != "0" ) { throw new Exception($stat); } return wrap_object($uid, $res->return); } public function deleteResource($au, $uid) { $params = array('sessId' => $this->sid, 'appUser' => $au, 'uid' => $uid); $res = $this->c->deleteResource($params); $stat = $this->_getStatus($res->return); if ( $stat != "0" ) { throw new Exception($stat); } return (int) $stat; } /* * attachment * */ public function addAttachment($au, $rid, $bin, $fname) { $params = array('sessId' => $this->sid, 'appUser' => $au, 'resourceId' => $rid, 'attachment' => $bin, 'filename' => $fname); $res = $this->c->addAttachment($params); return $res->return; } public function listAttachments($au, $rid) { $params = array('sessId' => $this->sid, 'appUser' =>$au, 'resourceId' => $rid); $res = $this->c->listAttachments($params); return to_object($res->return); } public function removeAttachment($au, $rid, $aid) { $params = array('sessId' => $this->sid, 'appUser' => $au, 'resourceId' => $rid, 'attachmentId' => $aid); $res = $this->c->removeAttachment($params); return $res->return; } public function getAttachment($au, $rid, $aid) { $params = array('sessId' => $this->sid, 'appUser' => $au, 'resourceId' => $rid, 'attachmentId' => $aid); $res = $this->c->getAttachment($params); return $res->return; } /* * search and vocabulary * */ public function getVocabulary($au, $tid, $field) { $p = array( 'sessId' => $this->sid, 'appUser' => $au, 'typeId' => $tid, 'field' => $field ); $res = $this->c->getVocabulary($p); return to_object($res->return); } public function listIdentifiers($au, $query) { $params = array('sessId' => $this->sid, 'appUser' => $au, 'query' => $query); $res = $this->c->listIdentifiers($params); return to_object($res->return); } public function search() { } public function list_() { } public function modifySchema() { $schema = file_get_contents('krihvel.schema'); $params = array('sessId' => $this->sid, 'schema' => $schema); $res = $this->c->modifySchema($params); $stat = $this->_getStatus($res->return); if ( $stat != "0" ) { throw new Exception($stat); } return (int) $stat; } /* * helpers * */ private function _getStatus($inp) { preg_match($this->statusPat, $inp, $matches); return $matches[1]; } private function _getSessionId($inp) { preg_match($this->sessIdPat, $inp, $matches); return $matches[1]; } } ?>