'); define('HR','
'); define('ERROR','Error : '); $loading = true; $path = ini_get('include_path'); $ret = ini_set('include_path', dirname(__FILE__) . '/pear/'.PATH_SEPARATOR.$path); if($ret && file_exists(dirname(__FILE__) .'/pear/HTTP/Request.php') ){ require_once(dirname(__FILE__) . '/pear/HTTP/Request.php'); } else { printf('

Impossible to load required libraries (PEAR and PEAR/HTTP/Request)!
Please check if ./pear exist or install this libraries in you php\'s share directory.

'); $loading=false; } abstract class heiv_gallery_3_render { protected $pattern_css_photo=''; protected $patter_js_gallery=''; function get_render($infos) { $ret = ''; if( !is_null($infos) && !is_null($this->options) ) { if( isset($infos->entity->type) ) { switch($infos->entity->type) { case 'photo' : { $ret = $this->get_render_photo($infos); break; } case 'album' : { $ret = $this->get_render_album($infos); break; } case 'movie' : { break; } } } } return $ret; } function get_render_photo($infos) { return null; } function get_render_album($infos){ return null; } function get_css_photo(){ return null; } function get_js_gallery(){ return null ;} function get_deco_start(){ return null ;} function get_deco_end(){ return null ;} public function register_wp_css(){} public function register_wp_js(){} } class heiv_gallery_3 { protected $ticket=''; protected $options = null; protected $valid_render = array('default','smoothgallery','lightbox'); protected $fct_required = array( 'get_render_photo' => 1, 'get_render_album' => 1, 'get_css_photo' => 0, 'get_js_gallery' => 0, 'register_wp_css' => 0, 'register_wp_js' => 0 ); protected $renderer=null; protected $use_tree=false; function __construct() { $this->options = get_option('HeivGallery3PP'); if(is_null($this->options['g3_valid_skins'])) $this->options['g3_valid_skins'] = $this->validate_skins(0); // the_posts gets triggered before wp_head in order to load CSS + JS if(!strstr($_SERVER['REQUEST_URI'],'wp-admin')) { add_filter('the_posts', array($this,'conditionally_add_scripts_and_styles') ); } if( isset($this->options['registry_tree']) && $this->options['registry_tree']) { $url = str_replace('/rest/item','/rest/tree',$url); $url = $url . (strpos($url,'?')>0?'':'?') . '&depth=1'; $this->use_tree=true; } add_action( 'admin_init', array( $this, 'action_admin_init' ) ); // hook for the ajax response during edit posts and pages add_action('wp_ajax_heiv_gallery_3_populate_tree', array($this,'ajax_hg3_populate_tree_cb') ); add_action('wp_ajax_heiv_gallery_3_populate_photos', array($this,'ajax_hg3_populate_photos_cb') ); add_action('wp_ajax_heiv_gallery_3_get_renderer', array($this,'ajax_hg3_get_renderer') ); // hook for the plugin's config add_action('admin_menu',array($this,'config_admin_menu') ); // hook for the binding tag add_shortcode('hgallery3',array($this,'hg3_display') ); } function conditionally_add_scripts_and_styles($posts){ if (empty($posts)) return $posts; $render_found = array(); array_push($render_found,$this->options['g3_render_single_img']); foreach ($posts as $post) { $matches=array(); preg_match_all("#\[hgallery3.*\]#",$post->post_content,$matches); foreach($matches[0] as $match) { $matches2 = array(); if( 1 <= preg_match_all("#render=\"(.*)\"#",$match,$matches2) ) { foreach($matches2[1] as $m) { array_push($render_found,$m); } unset($matches2); } } unset($matches); } $render_found = array_unique($render_found); if (!is_null($render_found) ) { foreach($render_found as $render) { if(is_file(dirname(__FILE__).'/skins/'.$render.'/'.$render.'.inc.php')) { require_once(dirname(__FILE__).'/skins/'.$render.'/'.$render.'.inc.php'); $methods = get_class_methods('heiv_gallery_3_render_' . $render ); if( in_array( 'register_wp_js' , $methods)) { $class = 'heiv_gallery_3_render_' . $render; $instance = new $class($this->options); $instance->register_wp_js(); unset($class); unset($instance); } if( in_array( 'register_wp_css' , $methods )) { $class = 'heiv_gallery_3_render_' . $render; $instance = new $class($this->options); $instance->register_wp_css(); unset($class); unset($instance); } unset($render); } } } return $posts; } function action_admin_init() { // only hook up these filters if we're in the admin panel, and the current user has permission if ( current_user_can( 'edit_posts' ) || current_user_can( 'edit_pages' ) ) { add_filter( 'mce_buttons', array($this,'filter_mce_button') ); add_filter( 'mce_external_plugins', array($this,'filter_mce_plugin') ); wp_enqueue_script( 'json2' ); wp_enqueue_style( 'hg3_style',plugin_dir_url(__FILE__).'css/admin.css' ); if(strstr($_SERVER['REQUEST_URI'],'wp-admin/options-general.php?page=heiv-gallery_3.php')) { wp_enqueue_script('jquerytools',plugin_dir_url(__FILE__).'js/jquery.tools.min.js'); } } } function config_admin_menu(){ add_options_page('Gallery 3 Configuration','Gallery 3',9,basename(__FILE__),array($this,'admin_config_page') ); } function filter_mce_button( $buttons ) { // add a separation before our button, here our button's id is "mygallery_button" array_push( $buttons, '|', 'heiv_gallery_3_button' ); return $buttons; } function filter_mce_plugin( $plugins ) { // this plugin file will work the magic of our button $plugins['heiv_gallery_3'] = plugin_dir_url( __FILE__ ) . 'heiv_gallery_3_mce_editor.js'; return $plugins; } function debug_settings() { /* foreach($_POST as $k=>$v) echo '
' . $k . '=' . $v; */ echo ''; if(isset($_POST['g3_host']) && $_POST['g3_host']!='') { $host = $_POST['g3_host']; $error = $guest = false; $ticket = $user = $password = null; $ret=''; if($this->test_url($host)) { $ret.= NL . $host . ' is OK.' ; $guest = $_POST['g3_guest_access']; if($guest=='true'){ if($this->test_url($host.'/rest/item/1')){ } else{ $ret.= HR . sprintf(__('Guest isn\'t allowed to make RESTFul query on this gallery3 - %s -)'),$host); $error = true; } } else { if(isset($_POST['g3_user']) && $_POST['g3_user']!='') { $user = $_POST['g3_user']; } if(isset($_POST['g3_passwd']) && $_POST['g3_passwd']!='') { $password=$_POST['g3_passwd']; } if($password==''){ $ret .= HR . __('Warning : Password is empty.').NL; } if($user!=''){ $ticket = $this->get_ticket_g3($host.'/rest/',$user,$password); if( is_string($ticket)===true && $ticket != '') { $ret .= HR . sprintf(__('Get ticket for user %s : %s'),$user,$ticket); } else{ $ret .= HR . ERROR . sprintf(__('Are you sure that the user with the username "%s" can make restful query on %s ?
Check the username and the password please and retry this test.'),$user,$host); $error = true; } } else { $ret .= HR . ERROR . __('User is empty, User must be set in order to retrieve a Gallery3 ticket.'); $error = true; } $ticket_post = ''; if(isset($_POST['g3_ticket_rest_api']) && $_POST['g3_ticket_rest_api']!='') { $ticket_post = $_POST['g3_ticket_rest_api']; } if($ticket !== $ticket_post){ $ret .= HR . __('Warning : Ticket recently get from Gallery3 is not the same as save before.'); $ret .= NL . sprintf(__('Saved ticket : %s') , $ticket_post); $ret .= NL . sprintf(__('Debug ticket : %s') , $ticket); } } if($error==false) { # $ret .= HR . 'RESTFul API version : ' . $this->get_version_API($host); $url_root = $host.'/rest/item/1'; $root = $this->get_infos($url_root,$ticket); $ret .= HR . 'Query Root Album of Gallery3 and get : '; $cover = $this->get_infos($root[$url_root]->entity->album_cover,$ticket); if($cover){ $ret .= sprintf('
Cover of Root Album
',$cover[$root[$url_root]->entity->album_cover]->entity->thumb_url_public); } $ret .= NL . 'Pictures in the Root Album :'; foreach($root[$url_root]->members as $member_url){ if($member_url==$url_rool) continue; if(!isset($root[$member_url])) $root[$member_url] = $this->get_infos($member_url,$ticket); if($root[$member_url]){ $ret .= sprintf('
%s
',$root[$member_url]->entity->thumb_url_public,$root[$member_url]->entity->name); } } } } else { $ret .= NL . ERROR . $host . ' is not accessible. Stop testing here, check your configuration and fix it !'; $error = true; } } else { $ret .= 'The value of "Host" is empty'; } if($error==false) $ret .= NL . 'The settings seem to be OK'; $ret .= NL . NL . 'This is the end of tests.'; echo '

Testing of the settings

' . $ret; } function get_options() { $options = array(); foreach($_POST as $k=>$v) if(strpos(trim($k),'g3_')!==FALSE) { if($k!=='g3_passwd') { $options[trim($k)]=trim($v); } } return $options; } function get_registry_tree() { $url = sprintf('%s/rest/registry',$this->options['g3_host']); $info = $this->get_infos($url,''); $registry_tree=false; if( $info ){ $registry_tree = in_array('tree',$info); } return $registry_tree; } function admin_config_page(){ if (!current_user_can('manage_options')) die(__('You cannot edit the Gallery 3 options.')); $options = array(); $action_buttons = isset( $_POST['action_buttons'] )?trim($_POST['action_buttons']):''; switch( $action_buttons ){ case 'reset_settings': echo 'Reset settings'; break; case 'debug_settings': $this->debug_settings(); $options = $this->get_options(); unset($this->options); $this->options = get_option('HeivGallery3PP'); foreach($options as $k=>$v) { $this->options[$k] = $v; } break; case 'save_settings': $options = $this->get_options(); if($options['g3_guest_access']=='false') { if($_POST['g3_passwd']!='' ) { $options['g3_ticket_rest_api'] = $this->get_ticket_g3($options['g3_host'].'/rest/',$_POST['g3_user'],$_POST['g3_passwd']); if( empty( $options['g3_ticket_rest_api'] ) ) { echo HR . ERROR . sprintf(__('Are you sure that the user with the username "%s" can make restful query on %s ?
Check the username and the password please and retry this test.'),$_POST['g3_user'],$options['g3_host']); $options['g3_ticket_rest_api']=''; } } } $options['g3_valid_skins'] = $this->validate_skins(0); $options['registry_tree'] = $this->get_registry_tree(); update_option('HeiVGallery3PP', $options); unset($this->options); $this->options = $options; break; case 'test_skins': $this->validate_skins(1); default: $this->options = get_option('HeivGallery3PP'); } ?>

Gallery 3 Configuration

General

options['g3_guest_access']=='true') printf(''); ?>
(info)
(info) options['g3_guest_access']=='true') echo ' checked="checked" '; ?>/> */?>
(info)
(info)
(info)
options['g3_valid_skins'] ) ) { ?> (info) %s %s', __('Option not available.') , __('Click on the "Test skins" button for debugging.')); } ?>

Skins

options['g3_valid_skins'] ) ) { $names_tabs = $panes_skins = ''; foreach($this->options['g3_valid_skins'] as $template) { $names_tabs .= sprintf('
  • %s
  • ',$template); require_once(dirname(__FILE__).'/skins/'.$template.'/'.$template.'.inc.php'); $class_renderer = 'heiv_gallery_3_render_'.$template; $renderer = new $class_renderer($this->options,null); $panes_skins .= '
    '.$renderer->get_form_conf().'
    '; } printf('
      %s
    %s
    ', $names_tabs,$panes_skins); } else { printf('
  • %s %s
  • ',__('There is "0" skin to configure.') , __('Click on the "Test skins" button for debugging')); } ?>

    In order to allow guest querying Restful's Gallery3, you have to read this documentation and apply the instructions : Security and Authentication of Restful's Gallery3
    For instance :
    User for querying Restful's Gallery3 website
    RESTFul API ticket will be used for query the RESTful Interface of Gallery3.
    If you want to change it, edit the user and the password and save the configuration.
    Password of the user who can query Restful's Gallery3 website. The password isn't store on the WP database !
    Used skin for the rendering if an item is only a picture.
    setMethod(HTTP_REQUEST_METHOD_POST); $req->addPostData('user',$user); $req->addPostData('password',$pwd); $req->addHeader('X-Gallery-Request-Method', 'post'); $response = $req->sendRequest(); if (PEAR::isError($response)) { //echo $response->getMessage(); } else { $ticket = json_decode( $req->getResponseBody() ); } } return $ticket; } function get_version_API($url){ $version = null; if($url) { $req = new HTTP_Request($url.'/rest/item/1'); $req->setMethod(HTTP_REQUEST_METHOD_GET); $req->addHeader("X-Gallery-Request-Key", $ticket); $req->addHeader("X-Gallery-Request-Method", 'get'); $req->sendRequest(); $head = $req->getResponseHeader(); if($head && isset($head['x-gallery-api-version'])){ $version = $head['x-gallery-api-version']; } } return $version; } function get_raw_infos($url,$ticket='') { $ret = null; if($url) { $req = new HTTP_Request($url); $req->setMethod(HTTP_REQUEST_METHOD_GET); $req->addHeader("X-Gallery-Request-Key", $ticket); $req->addHeader("X-Gallery-Request-Method", 'get'); $req->sendRequest(); $ret = json_decode($req->getResponseBody()); } return $ret; } function get_infos($url,$ticket='') { $retour=null; if($url) { $requested_url = $url; $ret = $this->get_raw_infos($url,$ticket); if( $this->use_tree && !empty($ret) ) { $retour = array(); foreach($ret->entity as $item) { $retour[$item->url] = $item; } foreach($retour as $item) { if(isset($item->entity->parent) && array_key_exists($item->entity->parent,$retour) ) { if( !isset($retour[$item->entity->parent]->members) || !is_array($retour[$item->entity->parent]->members ) ) $retour[$item->entity->parent]->members = array(); array_push($retour[$item->entity->parent]->members,$item->url); } } unset($ret); $retour['dtree']='TRUE'; $ret = $retour; } else { $retour = array(); $retour[$requested_url]=$ret; if(isset($ret->members) ) foreach($ret->members as $item ) { if($item) $retour[$item] = $this->get_raw_infos( $item, $this->options['g3_ticket_rest_api']); } } } return $retour; } function test_url($url,$ticket='') { $ret = false; if($url!=''){ try { $req = new HTTP_Request($url); $req->setMethod(HTTP_REQUEST_METHOD_GET); $response = $req->sendRequest(); if (PEAR::isError($response)) { return false; } else { return $req->getResponseCode()=='200'; } } catch (Exception $e){ $ret = false; } } return $ret; } function ajax_hg3_get_renderer() { echo json_encode(array('renderer'=>$this->options['g3_valid_skins'])); die(); // it is normal to die here for ajax mode } function ajax_hg3_populate_photos_cb() { $photos = array(); extract(shortcode_atts(array( 'id' => '1', 'limit' => 15, 'start' => 0 ), $attrs)); if(isset($_POST['id']) && $_POST['id']!='') $id = $_POST['id']; if(isset($_POST['limit']) && $_POST['limit']!='') $limit = $_POST['limit']; if(isset($_POST['start']) && $_POST['start']!='') $start = $_POST['start']; $start_ = $start*$limit; $stop = ($start+1)*$limit; $nb_photos=0; $url = sprintf('%s/rest/item/%d?type=photo&num=%d&start=%d',$this->options['g3_host'],$id,$limit,$start); $info = $this->get_infos($url,$this->options['g3_ticket_rest_api']); foreach($info as $item_url=>$item ) { if( $item_url == $url ){ continue; } if( $item->entity->type=='photo') { if($start_ <= $nb_photos && $nb_photos <$stop){ array_push( $photos, array( 'id' => $item->entity->id, 'title' => $item->entity->name, 'type' => $item->entity->type, 'thumb' => $item->entity->thumb_url_public) ); } $nb_photos++; } } $nb_pages = ceil($nb_photos/$limit); echo json_encode(array('photos'=>$photos,'nb'=>$nb_photos,'nb_pages'=>$nb_pages,'req_p'=>$start)); die(); } function ajax_hg3_populate_tree_cb() { extract(shortcode_atts(array( 'id' => '1', 'limit' => 15, 'start' => 0 ), $attrs)); if(isset($_POST['id']) && $_POST['id']!='') $id = $_POST['id']; if(isset($_POST['limit']) && $_POST['limit']!='') $limit = $_POST['limit']; if(isset($_POST['start']) && $_POST['start']!='') $start = $_POST['start']; $url = sprintf('%s/rest/item/%d',$this->options['g3_host'],$id); $info = $this->get_infos($url,$this->options['g3_ticket_rest_api']); $ret = array(); if($info){ $ret['title'] = $info[$url]->entity->title; $ret['members'] = array(); $ret['req'] = $id; $nb_photos = 0; $start_ = $start*$limit; $stop = ($start+1)*$limit; foreach($info as $item_url => $item){ if( $item_url == $url ){ continue; } if( $item->entity->type=='album') { array_push( $ret['members'], array( 'id' => $item->entity->id, 'title' => $item->entity->name, 'type' => $item->entity->type, 'thumb' => $item->entity->thumb_url_public) ); continue; } } $ret['pid'] = substr(strrchr($info[$url]->entity->parent, '/'), 1); $ret['req_p'] = $start; } else { $ret['error']=1; } echo json_encode($ret); die(); // it is normal to die here for ajax mode } function hg3_display($attrs,$content,$code) { extract(shortcode_atts(array( 'target'=>'_blank', 'id' => '1', 'width' => $this->options['g3_width'], 'height' => $this->options['g3_height'], 'direct_link' => $this->options['g3_smoothgallery_directLink'], 'render' => 'smoothgallery' ), $attrs)); if(is_numeric($height)){ $height.='px'; } else { $height = $this->options['g3_height'].'px'; } if(is_numeric($width)){ $width.='px'; } else { $width = $this->options['g3_width'].'px'; } if($direct_link=='false' || $direct_link=='true') { } else { $direct_link = isset($this->options['g3_smoothgallery_directLink'])?$this->options['g3_smoothgallery_directLink']:'false'; } $class_renderer = null; if( ! in_array($render,$this->valid_render) && is_file(dirname(__FILE__) . '/skins/'.$render.'inc.php') ){ // default renderer $render = 'smoothgallery'; $class_renderer = 'heiv_gallery_3_render_'.$render; } else { $class_renderer = 'heiv_gallery_3_render_'.$render; } if( isset($_REQUEST['target']) ) { $this->options['target'] = $_REQUEST['target']; } $str = ''; $str_css=''; $target_id = sprintf('%.9s',md5($url.$class_renderer)); if( isset($this->options['target']) && $this->options['target'] == $target_id && isset($_REQUEST['id']) && is_numeric($_REQUEST['id'] ) ) { $id = $_REQUEST['id']; } $url = sprintf('%s/rest/item/%d',$this->options['g3_host'],$id); $nomtime=function_exists(microtime); $rdm_id = sprintf('%.6s',md5(($nomtime?microtime():time()).rand(0,100))); $this->options['rdm_id']=$rdm_id; $this->options['permalink'] = get_permalink( $wp_query->post->ID ); $infos_raw = $this->get_infos($url,$this->options['g3_ticket_rest_api']); if($infos_raw && $class_renderer){ $infos = $infos_raw[$url]; require_once(dirname(__FILE__).'/skins/'.$render.'/'.$render.'.inc.php'); $renderer = new $class_renderer($this->options,$attrs); switch($infos->entity->type){ case 'album': if(sizeof($infos->members)>1 ){ $str .= $renderer->get_deco_start(); foreach($infos->members as $member){ $info = $this->get_infos($member,$this->options['g3_ticket_rest_api']); if($info){ $info[$member]->target = $target_id; $str .= $renderer->get_render( $info[$member] ); } } $str_css = $renderer->get_js_gallery(); $str .= $renderer->get_deco_end(); } elseif (sizeof($infos->members)==1) { $info = $this->get_infos($infos->members[0],$this->options['g3_ticket_rest_api']); if($info){ $class_renderer = 'heiv_gallery_3_render_'.$this->options['g3_render_single_img']; require_once(dirname(__FILE__).sprintf('/skins/%1$s/%1$s.inc.php',$this->options['g3_render_single_img'])); $renderer = new $class_renderer($this->options,$attrs); $str_css = $renderer->get_css_photo(); if($info[$infos->members[0]]->entity->type=='photo') $str = $renderer->get_render($info[$infos->members[0]]); else { $cover = $info[$infos->members[0]]->entity->album_cover; $str = $renderer->get_render($info[$cover]); } } } else { $str = __('This gallery is empty.
    Back.'); } break; case 'photo': $class_renderer = 'heiv_gallery_3_render_'.$this->options['g3_render_single_img']; require_once(dirname(__FILE__).sprintf('/skins/%1$s/%1$s.inc.php',$this->options['g3_render_single_img'])); $renderer = new $class_renderer($this->options,$attrs); $str_css = $renderer->get_css_photo(); $str = $renderer->get_render($infos); break; } $str = sprintf('%1$s',$str_css,$rdm_id,$str); } return $str; } function validate_skins($debug) { $valid_skins = array(); $dir = dirname( __FILE__ ) . '/skins'; if(is_dir($dir)) { if ($dh = opendir($dir)) { while (($file = readdir($dh)) !== false) { if( $file == '.' || $file == '..' ) continue; else { $filename = $dir.'/'.$file.'/'; $class = $filename.$file.'.inc.php'; if(is_dir($filename) && is_file($class) ) { if($debug) echo "
    ======== skin : $file ========="; ob_start(); readfile($class); $content_file = ob_get_contents(); ob_end_clean(); $ret = $this->validate_class($content_file); if( $ret['valid'] ) { if($debug) echo NL . __('skin ok.'); array_push($valid_skins,$file); } else { if($debug) { echo NL . __('skin is not valid because : '; } } unset($ret); } } } closedir($dh); } } return $valid_skins; } function validate_class($str) { $valid = true; $errors = array(); $matches=array(); // verification que la classe a valider est bien une heritiere de heiv_gallery_3_render if( preg_match_all( '#class(.*)extends(.*){#',$str,$matches ) ) { $class = trim($matches[1][0]); $parent = trim($matches[2][0]); unset($matches); $matches=array(); if( $parent=='heiv_gallery_3_render') { // verification des fonctions minimales pour l utilisation du skin if( preg_match_all( '#function(.*)\((.*)\).*{#',$str,$matches ) ) { array_walk($matches[1], create_function('&$item,$key' ,' $item = trim($item);' ) ); array_walk($matches[2], create_function('&$item,$key' ,' $item = trim($item);' ) ); $diff = array_diff(array_keys($this->fct_required),$matches[1]); $inter_sec = array_intersect(array_keys($this->fct_required),$matches[1]); if(sizeof($inter_sec)!=sizeof($this->fct_required) ) { //array_push($errors,array('value'=>$diff,'msg'=>__('Methodes manquantes : %s' ) ); array_push($errors,array('value'=>$diff,'msg'=>_n('Missing function : %s', 'Missing functions : %s',sizeof($diff) ) ) ); $valid = false; } // verification du nombre de parametres des fonctions for($i=0;$ifct_required[$matches[1][$i]]) { array_push( $errors,array('value'=>array($matches[1][$i],$this->fct_required[$matches[1][$i]],sizeof(explode(',',$matches[2][$i])),$matches[2][$i]),'msg'=> __('Number of required variables for function %s : %s / number of getted variables from skin : %s ( %s )' ) ) ); $valid = false; } } } } else { array_push( $errors, array('value'=>array($class),'msg'=>__(' The skin %s doesn\'t declare the minimum required functions.' ) ) ); $valid = false; } } else { array_push( $errors, array('value'=>array($class,$parent),'msg'=> __(' The skin %s isn\'t a child of : heiv_gallery_3_render ( parent = %s ).' ) ) ) ; $valid = false; } } return array('valid'=>$valid,'errors'=>$errors); } }; if($loading){ $g3 = new heiv_gallery_3(); } ?>