get_source_params($source_id); if ($params['PARAMETERS']) { $vars=unserialize($params['PARAMETERS']); $params['PARAMETERS']=$vars; } return $params; } public function source_get_property_form($source_id) { global $charset; $params=$this->get_source_params($source_id); if ($params['PARAMETERS']) { //Affichage du formulaire avec $params['PARAMETERS'] $vars=unserialize($params['PARAMETERS']); foreach ($vars as $key=>$val) { global $$key; $$key=$val; } } $form="
"; $form .= ""; return $form; } //Nécessaire pour passer les valeurs obtenues dans form_pour_maj_entrepot au javascript asynchrone public function get_maj_environnement($source_id) { // global $form_from; // global $form_until; // global $form_radio; // $envt=array(); // $envt['form_from']=$form_from; // $envt['form_until']=$form_until; // $envt['form_radio']=$form_radio; return $envt; } public function maj_entrepot($source_id,$callback_progress='',$recover=false,$recover_env='') { global $dbh, $charset; global $form_from, $form_until, $form_radio; $this->callback_progress = $callback_progress; $params = $this->unserialize_source_params($source_id); $p = $params['PARAMETERS']; $this->source_id = $source_id; $this->n_recu = 0; $this->n_total = 0; //Connexion $zot = new zotero_protocol($p,$charset); //Récupération des clés d'items $tab_items_keys = array(); if (count($p['zotero_collections'])) { foreach($p['zotero_collections'] as $k=>$collection_key) { $tik = array(); $tik = $zot->get_items_keys($collection_key); if (count($tik)) { $tab_items_keys = array_merge($tab_items_keys, $tik); } } } else { $tab_items_keys = $zot->get_items_keys(); } //Nb items au total $this->n_total = count($tab_items_keys); //Récupération des items $tab_sync_items = array(); foreach($tab_items_keys as $k=>$item_key) { $item = $zot->get_item($item_key); $si = $this->rec_record($item); if ($si) { $this->n_recu++; $this->progress(); $tab_sync_items[]=$si; } } //Suppression des items non synchronisés $str_sync_items = ''; if(count($tab_sync_items)) { $str_sync_items = implode('","',$tab_sync_items); } if($str_sync_items) { $q = "delete from entrepot_source_".$this->source_id." where ref not in (\"".$str_sync_items."\")"; mysql_query($q,$dbh); } else { $q = "delete from entrepot_source_".$this->source_id; mysql_query($q,$dbh); } $q = "delete from external_count where source_id=".$this->source_id." and rid not in (select distinct recid from entrepot_source_".$this->source_id." )"; mysql_query($q,$dbh); return $this->n_recu; } } require_once("$class_path/curl.class.php"); class zotero_protocol { public $zotero_api_version = '2'; public $zotero_api_url = "https://api.zotero.org"; public $zotero_userid = ''; public $zotero_client_key = ''; public $channel = false; public $url = ''; public $params = ''; public $response = ''; public $error = false; public $error_msg = ''; public $result = false; public $charset = 'utf-8'; public $zotero_parser = null; public function __construct($params=array(),$charset='utf-8') { $this->zotero_userid = $params['zotero_userid']; $this->zotero_client_key = $params['zotero_client_key']; $this->charset = $charset; $this->channel = new Curl(); $this->channel->headers['Zotero-API-Version']=2; $this->zotero_parser = new zotero_parser(); } public function send_request($other_params=array()) { $this->error = false; $this->params = array(); $this->params['key'] = $this->zotero_client_key; $this->params['version'] = $this->zotero_api_version; if (is_array($other_params) && count($other_params)) { foreach($other_params as $k=>$v) { $this->params[$k]=$v; } } $rcurl = $this->channel->get($this->url,$this->params); if ($rcurl->headers['Status-Code']!='200') { $this->error = true; $this->error_msg = $rcurl->headers['Status']; } else { $this->response = $rcurl->body; } } public function get_collections() { $zp = $this->zotero_parser; $zp->reset('collections'); $this->result = array(); $this->url = $this->zotero_api_url."/users/".$this->zotero_userid."/collections?"; $this->send_request(); if (!$this->error) { try { $zpr = $zp->parse($this->response); if (count($zpr)) { foreach($zpr as $k=>$v) { $title = $v['title']; $id = $v['zapi:key']; if ($this->charset != 'utf-8') { $title = utf8_decode($title); } $this->result[$id] = $title; } natsort($this->result); } } catch (Exception $e) { } } return $this->result; } public function get_items_keys($collection_key='') { $this->result = array(); $this->url = $this->zotero_api_url."/users/".$this->zotero_userid.(($collection_key)?"/collections/".$collection_key:'')."/items?format=keys&itemType=-attachment%20||%20note"; $this->send_request(); if (!$this->error) { $this->result = array_filter(explode(chr(0x0A),$this->response)); } return $this->result; } public function get_childrens_keys($item_key='') { $this->result = array(); if ($item_key) { $this->url = $this->zotero_api_url."/users/".$this->zotero_userid."/items/".$item_key."/children?format=keys"; $this->send_request(); if (!$this->error) { $this->result = array_filter(explode(chr(0x0A),$this->response)); } } return $this->result; } public function get_children($item_key='') { $zp = $this->zotero_parser; $zp->reset('item'); $this->result = array(); if ($item_key) { $this->url = $this->zotero_api_url."/users/".$this->zotero_userid."/items/".$item_key."?content=json"; $this->send_request(); if (!$this->error) { try { $zpr = $zp->parse($this->response); if($zpr[0]['content']['itemType']=='attachment' && $zpr[0]['content']['linkMode']=='imported_file') { $zpr[0]['content']['url']=$this->zotero_api_url."/users/".$this->zotero_userid."/items/".$zpr[0]['zapi:key']."/file?key=".$this->zotero_client_key; } $this->result = $zpr[0]; } catch(Exception $e) {} } } return $this->result; } public function get_item($item_key='') { $zp = $this->zotero_parser; $zp->reset('item'); $this->result = array(); if ($item_key) { $this->url = $this->zotero_api_url."/users/".$this->zotero_userid."/items/".$item_key."?content=json"; $this->send_request(); if (!$this->error) { try { $zpr = $zp->parse($this->response); if ($zpr[0]['zapi:numChildren']) { $childrens = $this->get_childrens_keys($zpr[0]['zapi:key']); foreach($childrens as $k=>$v) { $zpr[0]['attachments'][$k]=$this->get_children($v); } } $this->result = $zpr[0]; } catch(Exception $e) { } } } return $this->result; } public function get_file($key) { $this->url = $this->zotero_api_url."/users/".$this->zotero_userid."/items/$key/file?"; $this->send_request(); if (!$this->error) { } } } class zotero_parser { public $parser; public $t=array(); public $t_i=0; public $to_parse='item'; public $prev_tag=''; public $path_tag=array(); public $text=''; public $charset = 'utf-8'; public function __construct ($to_parse='item', $charset='utf-8') { $this->charset = $charset; $this->to_parse = $to_parse; } public function reset($to_parse='item', $charset='utf-8') { $this->t = array(); $this->t_i= 0; $this->to_parse = $to_parse; $this->prev_tag = ''; $this->path_tag = array(); $this->text = ''; $this->charset = $charset; } public function parse ($xml) { $this->parser=xml_parser_create($this->charset); xml_set_object ($this->parser, $this); xml_parser_set_option ($this->parser, XML_OPTION_CASE_FOLDING, FALSE); xml_parser_set_option ($this->parser, XML_OPTION_SKIP_WHITE, TRUE); xml_set_element_handler ($this->parser, 'tag_start', 'tag_end'); xml_set_character_data_handler ($this->parser, 'texte'); if ( !xml_parse ($this->parser, $xml, TRUE)) { die (sprintf ("erreur XML %s à la ligne: %d", xml_error_string (xml_get_error_code ($this->parser)), xml_get_current_line_number ($this->parser))); } xml_parser_free ($this->parser); return ($this->t); } public function tag_start ($parser, $tag, $att) { $this->prev_tag=end($this->path_tag); $this->path_tag[]=$tag; $this->text=''; } public function tag_end ($parser, $tag) { if ( !count ($this->path_tag)) return; $this->text=trim($this->text); switch ($this->to_parse) { case 'collections' : switch ($tag) { case 'entry' : $this->t_i++; break; case 'title' : if ($this->prev_tag=='entry' && $this->text!=='') { $this->t[$this->t_i][$tag] = $this->text; } break; case 'zapi:key' : if ($this->prev_tag=='entry' && $this->text!=='') { $this->t[$this->t_i][$tag] = $this->text; } break; default : break; } break; case 'item' : switch ($tag) { case 'zapi:key' : case 'zapi:version' : case 'zapi:numChildren' : if ($this->text!=='') { $this->t[$this->t_i][$tag] = $this->text; } break; case 'content' : if ($this->text!=='') { $this->t[$this->t_i][$tag] = json_decode($this->text, true); } break; default : break; } break; } array_pop ($this->path_tag); } public function texte ($parser, $data) { if ( !count ($this->path_tag)) return; $this->text.=$data; } } ?>