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; } 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,0); if (count($tik)) { $tab_items_keys = array_merge($tab_items_keys, $tik); } } } else { $tab_items_keys = $zot->get_items_keys('',0); } //On double pour les groupes... if ($p['maj_groups']) { $groups = $zot->get_groups(); if (count($groups)) { foreach ($groups as $gr_id=>$gr_title) { $group_collections = $zot->get_group_collections($gr_id); $group_collections = array_intersect($group_collections,$p['zotero_groups_collections']); if (count($group_collections)) { foreach($group_collections as $k=>$group_collection_key) { $tik = array(); $tik = $zot->get_items_keys($group_collection_key,$gr_id); if (count($tik)) { $tab_items_keys = array_merge($tab_items_keys, $tik); } } } else { $tik = array(); $tik = $zot->get_items_keys('',$gr_id); if (count($tik)) { $tab_items_keys = array_merge($tab_items_keys, $tik); } } } } } //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 $item_key=>$item_url) { $item = $zot->get_item($item_key,$item_url); $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."\")"; pmb_mysql_query($q,$dbh); } else { $q = "delete from entrepot_source_".$this->source_id; pmb_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." )"; pmb_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 = array(); 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_groups() { $zp = $this->zotero_parser; $zp->reset('groups'); $this->result = array(); $this->url = $this->zotero_api_url."/users/".$this->zotero_userid."/groups?"; $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:groupID']; 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_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_group_collections($group_id = '') { $zp = $this->zotero_parser; $this->result = array(); if ($group_id) { $zp->reset('groups_collections'); $this->url = $this->zotero_api_url."/groups/".$group_id."/collections?"; $this->send_request(); if (!$this->error) { try { $zpr = $zp->parse($this->response); if (count($zpr)) { foreach($zpr as $k=>$v) { $id = $v['zapi:key']; $this->result[] = $id; } } } catch (Exception $e) { } } } return $this->result; } public function get_groups_collections($groups = array()) { $zp = $this->zotero_parser; $this->result = array(); if (count($groups)) { foreach ($groups as $gr_id=>$gr_title) { $zp->reset('groups_collections'); $this->url = $this->zotero_api_url."/groups/".$gr_id."/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] = $gr_title." : ".$title; } natsort($this->result); } } catch (Exception $e) { } } } } return $this->result; } public function get_items_keys($collection_key='', $group_id=0) { $this->result = array(); if (!$group_id) { $this->url = $this->zotero_api_url."/users/".$this->zotero_userid.(($collection_key)?"/collections/".$collection_key:'')."/items?format=keys&itemType=-attachment%20||%20note"; } else { $this->url = $this->zotero_api_url."/groups/".$group_id.(($collection_key)?"/collections/".$collection_key:'')."/items?format=keys&itemType=-attachment%20||%20note"; } $this->send_request(); if (!$this->error) { $tmp_array = array_filter(explode(chr(0x0A),$this->response)); if (count($tmp_array)) { foreach ($tmp_array as $key) { if (!$group_id) { $this->result[$key] = $this->zotero_api_url."/users/".$this->zotero_userid."/items/".$key; } else { $this->result[$key] = $this->zotero_api_url."/groups/".$group_id."/items/".$key; } } } } return $this->result; } public function get_childrens_keys($item_key='', $item_url='') { $this->result = array(); if ($item_key) { $this->url = $item_url."/children?format=keys"; $this->send_request(); if (!$this->error) { $tmp_array = array_filter(explode(chr(0x0A),$this->response)); if (count($tmp_array)) { foreach ($tmp_array as $key) { $this->result[$key] = str_replace($item_key,$key,$item_url); } } } } return $this->result; } public function get_children($item_key='', $item_url='') { $zp = $this->zotero_parser; $zp->reset('item'); $this->result = array(); if ($item_key) { $this->url = $item_url."?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']=$item_url."/file?key=".$this->zotero_client_key; } $this->result = $zpr[0]; } catch(Exception $e) {} } } return $this->result; } public function get_item($item_key='', $item_url='') { $zp = $this->zotero_parser; $zp->reset('item'); $this->result = array(); if ($item_key) { $this->url = $item_url."?content=json"; $this->send_request(); if (!$this->error) { try { $zpr = $zp->parse($this->response); if ($zpr[0]['zapi:numChildren']) { $childrens = $this->get_childrens_keys($item_key,$item_url); foreach($childrens as $key=>$url) { $zpr[0]['attachments'][]=$this->get_children($key, $url); } } $this->result = $zpr[0]; } catch(Exception $e) { } } } return $this->result; } } 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); unset($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' : case 'groups_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 'groups' : 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:groupID' : 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; } } ?>