connect(); * ... do something... * $ws->disconnect(); * */ class DummyWaramu { function __call($name, $arguments) { } } function _helper_recur(&$result, $node) { /* if ( !$result['@schema'] && $node->getAttribute('xmlns')) { $result['@schema'][] = $node->getAttribute('xmlns'); }*/ if (!$result['@schema'] && $node->namespaceURI) { $result['@schema'][] = $node->namespaceURI; } $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; } function wrap_object($uid, $xmlstring) { $arr = to_object($xmlstring); return new WaramuResource($uid, $arr, $xmlstring); } class WaramuResource { public $ds; public $source; public $uid = null; public $defaultlangs = array('et', 'en'); public $isLocal = false; function __construct($uid, $arr, $xm) { $this->uid = $uid; if ( is_null($arr) ) { $arr = to_object($xm); } $this->ds = $arr; $this->source = $xm; } function getSchema() { if ( array_key_exists('schema', $this->ds)) { // old infos/xml $typeid = $this->ds['schema'][0][0]; if ($typeid == "5") { return "http://koolielu.ee/schemas/Material"; } elseif ($typeid == "2") { return "http://koolielu.ee/schemas/Reference"; } } if ( $this->ds == "") { return $_SESSION['schema']; } if ($this->ds['data'][0]['@schema'][0] == "") { return $_SESSION['schema']; } return $this->ds['data'][0]['@schema'][0]; } function getAllLanguages() { if ( is_null($this->uid) ) { return $this->defaultlangs; } $langs = array(); $w = waramu_get(); $td = $w->describeType($this->getSchema()); foreach( $td->listFields() as $field ) { $name = $field['@attributes']['name']; if ( $field['@attributes']['multiLingual'] == '1' ) { $x = $this->ds['data'][0][$name]; foreach($x as $fvals) { if ( !in_array($fvals['@attributes']['lang'], $langs) ) { $langs[] = $fvals['@attributes']['lang']; } } } } return $langs; } function get($f, $lang=null) { if ( is_null($this->uid) ) { if ( isset($_SESSION[$f]) ) return array($_SESSION[$f]); return array(""); } $x = $this->ds['data'][0][$f]; $v = null; if ( !is_null($lang) ) { foreach($x as $y) { if ( $y['@attributes']['lang'] == $lang ) { $v = $y; } } if ( is_null($v) ) { $v = array(""); } } else { $v = $x[0]; } $val = $v; if ( is_array($v) ) { if ( array_key_exists("value", $v) ) { $val = $v['value']; } } unset($val['@schema']); return $val; } /** * get multivalued field as an array * */ function getMVArray($fname, $lang=null) { $r = array(); $vals = $this->get($fname, $lang); foreach($vals as $v) { $r[] = $v[0]; } return $r; } function getMetadataLanguage() { return $this->ds['data'][0]['mdLanguage'][0][0]; } function getTitle($getfirst=false) { //$mdl = $this->getMetadataLanguage(); $mdl = "et"; $got = false; $res = ""; if ( !$this->ds['data'][0]['title'] ) { /*translation:(untitled)*/ return elgg_echo("koolielu:resource_untitled"); } foreach ($this->ds['data'][0]['title'] as $tles) { if ( $getfirst ) { return $tles[0]; } if ( !$got && $tles['@attributes']['lang'] == $mdl ) { $res = $tles[0]; $got = true; } } if (!$got) $res = $this->getTitle(true); if ( strlen($res) == 0 ) { /*translation:(untitled)*/ return elgg_echo("koolielu:resource_untitled"); } return $res; } function getDescription($chars=null) { $mdl = "et"; //defaulting to ET $res = null; foreach ($this->ds['data'][0]['desc'] as $descs) { if ($descs['@attributes']['lang'] == $mdl) { $res = $descs[0]; } } if (is_null($res)) { foreach ($this->ds['data'][0]['desc'] as $descs) { if ($descs['@attributes']['lang'] == $this->getMetadataLanguage()) { $res = $descs[0]; } } } if (is_null($res)) { $res = ""; } if (!is_null($chars)) { $ds = $res; $ds = strip_tags($ds); if (mb_strlen($ds)>$chars) { $ds = mb_substr($ds, 0,$chars); $ds = mb_substr($ds, 0, mb_strrpos($ds, ' ')); $ds .= '...'; } return $ds; } return $res; } // returns array of all resource keywords (in every language) function getKeywords() { $langs = $this->getAllLanguages(); $keywords = array(); foreach ( $langs as $l ) { $kws = $this->get("keywords", $l); foreach( $kws as $kw) { $keywords[] = $kw[0]; } } return $keywords; } function getAuthor() { return $this->ds['data'][0]['vauthor'][0][0]; } function getURL() { return $this->ds['data'][0]['url'][0][0]; } function getIconURL() { global $CONFIG; if (!strcmp(getCollectionSchema(), $this->getSchema())) { if (count($this->getItems())>1) { return $CONFIG->url . "mod/waramu/views/default/_graphics/icons/collection_many.png"; } else { return $CONFIG->url . "mod/waramu/views/default/_graphics/icons/collection_one.png"; } } $lotype = $this->get("lotype"); $lotype = $lotype[0][0]; $iconed_types = array_keys(serveAllSuitableLotypes()); if (in_array($lotype, $iconed_types)) { return $CONFIG->url . "mod/waramu/views/default/_graphics/icons/lotype_" . str_replace(" ", "_", $lotype) . ".png"; } else { return $CONFIG->url . "mod/waramu/views/default/_graphics/icons/lotype_other.png"; } } function getLotypeLabel($lotype=null) { if (!$lotype) { $lotype = $this->get("lotype"); $lotype = $lotype[0][0]; } $iconed_types = serveAllSuitableLotypes(); $iconed_types_keys = array_keys($iconed_types); if (in_array($lotype, $iconed_types_keys)) { return $iconed_types[$lotype]; } else { return $iconed_types["other"]; } } function getDate() { $tim = strtotime($this->ds['created'][0][0]); return $tim; } function getCurriculumSubjects() { $csm = $this->get("curriculumSubject"); //Array ( [0] => Array ( [0] => 334901 ) ) //Array ( [0] => Array ( [0] => 334901 ) [1] => Array ( [0] => 140101 ) ) $res = array(); foreach ( $csm as $cs ) { if ( trim($cs[0]) ) { if ( substr($cs[0], 0, 1) != '@') { $res[] = getSubjectID(getCurriculumMainSubjectNS($cs['@schema'][0], $cs[0])); } } } return $res; } function hasAttachments() { $o1 = isset($this->ds['attachments']); if ( !$o1 ) { return false; } if ( count($this->ds['attachments']) == 1 && count($this->ds['attachments'][0]) == 0 ) { return false; } return $o1; } function getNumberOfAttachments() { return isset($this->ds['attachments'][0]['attachment']) ? count($this->ds['attachments'][0]['attachment']):0; } function listAttachments() { return $this->ds['attachments'][0]['attachment']; } /** * returns an owner of the metadata * */ function getOwner() { return $this->ds['acl'][0]['owner'][0][0]; } function getOwnerPrintable() { $own = $this->ds['acl'][0]['owner'][0][0]; $own = substr($own, 0, strpos($own, '@')); // TODO: check suffix $u = get_user_by_username($own); if ($u) return array($u, $u->name); return array(null, $own); } /** * returns a list of $sub who have $mode rights * $sub - groups or users * $mode - view, edit, down * example: getACL("groups", "edit") - returns a list of groups who have edit permission * */ function getACL($sub, $mode) { $r = array(); if ( !isset($this->ds['acl'][0][$mode]) ) { return $r; } else { $x = $this->ds['acl'][0][$mode][0][$sub][0]['value']; foreach ( $x as $gv ) { $g = $gv[0]; $r[] = $g; } } return $r; } /** * checks permission for $mode * $mode - view, edit, down * TODO: if view is restricted, this method will propably fail. * TODO: integrate isModerator in here * */ function hasPermissionTo($mode) { if ( !isLoggedIn() ) { if ( $mode == 'view' && count($this->getACL("groups", "view")) == 0 && count($this->getACL("users", "view")) == 0) { // no view restrictions set, go ahead return true; } return false; // deny } $wner = $this->getOwner(); $curruname = get_loggedin_user()->username; $w = waramu_get(); $identity = $w->Identify(); $suffix = $identity['identity'][0]['suffix'][0][0]; $curruname .= "@".$suffix; if ( $wner == $curruname ) return true; // owner always has permission $aclist = $this->getACL("users", $mode); foreach ($aclist as $ac ) { if ( $ac == $curruname ) return true; } $sublist = get_loggedin_user()->subjects; $aclist = $this->getACL("groups", $mode); // In case of just one subject, we will be dealing with a string if ($sublist && !is_array($sublist)) { // Now we make it a list $sublist = array($sublist); } foreach ( $sublist as $sub ) { foreach ($aclist as $ac) { if ( $sub."@".$suffix == $ac ) return true; } } if ( isEditor() ) { foreach ($aclist as $ac) { if ( $ac == "editor@".$suffix ) return true; } } return false; } function getAnnotationByName($name) { if ( !isset($this->ds['annotations']) ) { return null; } foreach ($this->ds['annotations'][0]['annotation'] as $anno) { if ( !strcmp($anno['name'][0][0], $name) ) { return new AnnotationWrapper($anno); } } return null; } function isPublished() { if ( $this->getAnnotationByName('koolielu') != null) { return true; } return false; } function getCollectionIDs() { $res = array(); if ( !isset($this->ds['collections']) ) { return $res; } foreach ($this->ds['collections'][0]['value'] as $coll) { $res[] = $coll[0]; } return $res; } /** * Collection only * */ function getItems() { $items = $this->get("items"); $fi = array(); foreach($items as $i) { //$c .= "
".$i[0]."
"." pos:".$i['@attributes']['position']; if (!is_array($i)) continue; $fi[$i[0]] = 0; $v = $i['@attributes']['position']; if (is_numeric($v)) $fi[$i[0]] = (int)$i['@attributes']['position']; } asort($fi); return $fi; } function renderEmbed() { return ''; } } class Waramu { public $statusPat = '/(.*?)<\/status>/'; public $uidPat = '/(.*?)<\/uid>/'; public $sessIdPat = '/(.*?)<\/sessionId>/'; private $c = NULL; private $a = NULL; public $sid = NULL; private $typeID = 4; private $_wloca = NULL; private $_wuser = NULL; private $_wpass = NULL; public function __construct($url, $user, $pass) { $this->_wloca = $url; $this->_wuser = $user; $this->_wpass = $pass; $this->c = new SoapClient($this->_wloca."/waramu?wsdl"); $this->a = new SoapClient($this->_wloca."/annotation?wsdl"); } /** * 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) { try { $res = $this->c->newSession(array('uid' => $uid, 'pwd' => $pwd)); } catch ( Exception $e) { return 1; } 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) { global $CONFIG; if ( file_exists($CONFIG->pluginspath.'waramu/schemas/'.md5($tid).'.xml') ) { return new Schema(to_object(file_get_contents($CONFIG->pluginspath.'waramu/schemas/'.md5($tid).'.xml'))); } else { $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 new Schema(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, $rid=null) { $params = array('appUser'=> $appUser, 'sessId' => $this->sid, 'data' => $metadata); // TODO: this is stpid if ( !is_null($rid) && strlen($rid) > 3 ) $params['rid'] = $rid; $res = $this->c->newResource($params); $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 == "1" ) { unset($_SESSION['waramu_session']); } if ( $stat != "0" ) { throw new Exception($stat); } $o = wrap_object($uid, $res->return); 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 to_object($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() { } /* * search and vocabulary * */ public function getVocabulary($au, $tid, $field) { $p = array( 'sessId' => $this->sid, 'appUser' => $au, 'field' => $field ); //$res = $this->c->getVocabulary($p); //return to_object($res->return); $q = ""; $q .= "<_field"; if ( !is_null($tid) ) { $q .= " xmlns=\"".$tid."\""; } $q .= ">".$field.""; $q .= '*'; $q .= ""; return $this->listIdentifiers($au, $q); } 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 reserveID() { $params = array('sessId' => $this->sid); $res = $this->c->reserveID($params); return to_object($res->return); } public function getUploadToken($au, $uid) { $params = array('sessId' => $this->sid, 'appUser' => $au, 'uid' => $uid); $res = $this->c->getUploadToken($params); return to_object($res->return); } public function search() { } public function list_() { } /* * 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]; } /** * Annotation * */ /** * $au - app user * $uid - waramu unique id of the resource * $data - array('name'=>'', 'value'=>'') * */ public function addAnnotation($au, $uid, $data) { $xd = ''; $xd .= ''.$data['name'].''; $xd .= ''.$data['value'].''; $xd .= ''; $params = array('sessId' => $this->sid, 'appUser' => $au, 'uid' => $uid, 'data' => $xd); $res = $this->a->addAnnotation($params); return $res->return; } public function createOrUpdateAnnotation($au, $uid, $data, $acl=null) { $xd = ''; $xd .= ''.$data['name'].''; $xd .= ''.$data['value'].''; if ($acl!=null) { $xd .= $acl; } $xd .= ''; $params = array('sessId' => $this->sid, 'appUser' => $au, 'uid' => $uid, 'data' => $xd); $res = $this->a->createOrUpdateAnnotation($params); return $res->return; } } ?>