_tonic->baseUrl.$_GET['owl-sameAs']; else return false; } function get($requestData = NULL, $requestDataType = NULL) { require("erdft_parser.php"); $stringTemplates = new stringTemplates(); $this->_smarty->register_resource('string', array(&$stringTemplates, 'getTemplate', 'getTimestamp', 'getSecure', 'getTrusted')); $resource =& Resource::find($this->_tonic, $this->getUrl()); $this->_smarty->assign_by_ref('this', $this); $this->_smarty->assign_by_ref('resource', $this); // deprecated $erdf = $resource->getBody(); $template = $this->getMetadata('template'); if ($template) { $erdf = $this->_smarty->fetch($template, $this->_url, $this->_url); } else { $erdf = $this->_smarty->fetch('tonic:_body', $this->_url, $this->_url); } $eRdfT = new eRDFT($erdf); //this is the regex for sparql_vars $data = $eRdfT->get_results(); //set Smarty variables $this->_smarty->assign('errors', $eRdfT->errors); $this->_smarty->assign('data', $data); foreach($data as $k => $v) $this->_smarty->assign($k,$v); $stringTemplates->template('erdft', $erdf); $this->_smarty->display("string:erdft"); } function post($requestData = NULL, $requestDataType = NULL) { parse_str(urldecode($requestData), $post); //makes $post the same as $_POST before tonic flattened it if($post['rdf:RDF']) { $iri = 'http://'.$_SERVER['SERVER_NAME'].$this->getFullUrl(); $redir = $this->getMetadata('redirect_url'); $redirect_url = ($redir)? $redir : $this->getUrl(); $form_url = ($this->getMetadata('form_url'))? $this->getMetadata('form_url'): $this->_tonic->basePath.$this->getUrl(); //process POST by fetching the html form, getting the allowed input names, and passing them through functions named in the class parameters require 'FormProcessor.class.php'; $formProcessor = new FormProcessor($post, $iri, $redirect_url); $processed_POST = $this->_process_POST($form_url, &$post, &$formProcessor); //convert data to rdf $rdf = $this->_array2rdf(array('rdf:RDF' => $post['rdf:RDF'])); //save the data if(empty($formProcessor->errors)) { require 'ArcAdapter.php'; $store = new ArcAdapter('api/api_config.php'); if ($store->insert($formProcessor->iri, $rdf) ) { $this->get(); } else { die("couldn't save ".htmlentities( $rdf)); } } else { var_dump($formProcessor->errors); $this->_smarty->assign('errors', $formProcessor->errors); } } return get(); } function _array2rdf($in) { $rdf=''; if(is_array($in)) { foreach($in as $k => $v) { if(is_string($v)) { if(substr($v,0,7)=='http://') { $rdf.= '<'.$k.' rdf:resource="'. $v.'"/>'; } else { $rdf.= '<'.$k.'>'. $v .''; } } else //$v is an array { $atts = array(); foreach(array_keys($v) as $ak) { if((strpos($ak,'xmlns') === 0 || strtolower($ak)== 'rdf:about') ) { $atts[] = $ak.'="'.$v[$ak].'"'; unset($v[$ak]); } } $space = (empty($atts))? '':' '; if(strtolower($k) == 'rdf:li') { foreach($v as $li) { $vx = $this->_array2rdf($li); $rdf.= '<'.$k.$space.implode(" \r\n",$atts).'>'. $vx .''; } } else { $vx = $this->_array2rdf($v); $rdf.= '<'.$k.$space.implode(" \r\n",$atts).'>'. $vx .''; } } } return $rdf; } else { return($in); } } function _escape_array($arr = array()) { $rs = array(); while(list($key,$val) = each($arr)) { if(is_array($val)) { $rs[$key] = $this->_escape_array($val); } else { $rs[$key] = htmlspecialchars(utf8_encode($val)); } } return $rs; } function _process_POST($url, $post, &$formProcessor) { $simple = file_get_contents($url); if(!$simple) $formProcessor->errors[]='Couldn\'t retrieve form page to parse.'; $p = xml_parser_create(); xml_parse_into_struct($p, $simple, $vals, $index); xml_parser_free($p); $previous_row_num = -2; //to get the previous row num foreach($vals as $r) { $previous_row_num++; if(isset($r['attributes']['NAME'])) //it's (probably) a form element { parse_str($r['attributes']['NAME'], $out); $template_post_array = array_merge_recursive($template_post_array, $out); preg_match_all('/([^\[\]]+)/', $r['attributes']['NAME'], $matches); //this builds up a path to the assoc array element as a string - eg: 'rdf:Rdf[foaf:name]' $path = ''; for($x = 0; $x < count($matches[1]); $x++) { $curr_key = $matches[1][$x]; $path.='["'.$curr_key.'"]'; } if(isset($r['attributes']['CLASS'])) //now set the classnames string as the value of the name associative array { preg_match_all('/f_([a-zA-Z_0-9&]+)/', $r['attributes']['CLASS'], $m); $functions = $m[1]; //get user value to be passed through functions eval('$user_value = $post'.$path.';'); if(!empty($functions)) { foreach($functions as $f) { //presumably calls the functions in the order they appear in the class string $identifier = $vals[$previous_row_num]['value']; $user_value = call_user_func(array(&$formProcessor, $f), $user_value, $identifier); } eval('$post'.$path.' = "'.$user_value.'";'); } } } } return $post; } } class stringTemplates { var $templates = array(); function getTemplate ($tplName, &$tplSource, &$smarty) { if(array_key_exists($tplName, $this->templates)) { $tplSource = $this->templates[$tplName]; return TRUE; } return FALSE; } function getTimestamp($tplName, &$tplTimestamp, &$smarty) { if(array_key_exists($tplName, $this->templates)) { $tplTimestamp = time(); return TRUE; } return FALSE; } function getSecure($tpl_name, &$smarty) { return TRUE; } function getTrusted($tpl_name, &$smarty) { } function template($tplName, $tpl) { if(empty($tplName)) { return FALSE; } $this->templates[$tplName] = $tpl; } } ?>