Announcement

  •  » Extensions
  •  » A Method To View Private Photos in WordPress using PiwigoPress

#1 2013-02-01 21:02:39

kenl
Member
2013-01-12
91

A Method To View Private Photos in WordPress using PiwigoPress

Hello,

I was interested in using my WordPress installation to pull and display photos from my Piwigo installation.  This is possible using PiwigoPress.  However, PiwigoPress only pulls photos that are public (that is, viewable by 'guest').  I set my Piwigo installation such that it requires a user to log in to see any photos.  (All my albums are marked private.)

So, I modified the PiwigoPress code to log into Piwigo before pulling photos.  This allows WordPress to pull and display photos that are marked as private.

As usual, I will say that I am PHP novice. But, if anyone is interested, the code below works ok. 


Code:

  
//------------------------------------------
//Log in to Piwigo
//------------------------------------------
$Curl_Session = curl_init($piwigo_url . "/ws.php?method=pwg.session.login");
$tmp_fname = tempnam("/tmp", "COOKIE");
curl_setopt ($Curl_Session, CURLOPT_COOKIEJAR, $tmp_fname);
curl_setopt ($Curl_Session, CURLOPT_POST, 1);
curl_setopt ($Curl_Session, CURLOPT_POSTFIELDS, "username=" . $username . "&password=" . $password);
curl_setopt ($Curl_Session, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt ($Curl_Session, CURLOPT_COOKIEFILE,  $tmp_fname);
curl_setopt($Curl_Session, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec ($Curl_Session);
  
//------------------------------------------
//Check Log In Status (optional)
//------------------------------------------
$Curl_Session = curl_init();
curl_setopt ($Curl_Session, CURLOPT_COOKIEJAR, $tmp_fname);
curl_setopt ($Curl_Session, CURLOPT_COOKIEFILE,  $tmp_fname);
curl_setopt($Curl_Session, CURLOPT_TIMEOUT, 5);
curl_setopt ($Curl_Session, CURLOPT_POST, 0);
curl_setopt($Curl_Session, CURLOPT_URL, $piwigo_url . "/ws.php?method=pwg.session.getStatus");
curl_setopt($Curl_Session, CURLOPT_USERAGENT, 'Mozilla/5.0');
curl_setopt($Curl_Session, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($Curl_Session, CURLOPT_FOLLOWLOCATION, 1);    
curl_setopt ($Curl_Session, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt ($Curl_Session, CURLOPT_SSL_VERIFYPEER, 0);

//------------------------------------------
//Get Photo Information
//------------------------------------------
curl_setopt($Curl_Session, CURLOPT_URL, '$piwigo_url . "/ws.php?method=pwg.images.getInfo&format=php&image_id=" . $id);
$response = @curl_exec($Curl_Session);
$status = @curl_getinfo($Curl_Session);  
@curl_close($Curl_Session);

Offline

 

#2 2013-02-09 00:27:53

Simencourt
Guest

Re: A Method To View Private Photos in WordPress using PiwigoPress

Hello,

What plugin file PiwigoPress do I copy your code?

Thanks.

-----

Bonjour,

Dans quel fichier du plugin PiwigoPress dois-je copier votre code ?

Merci.

 

#3 2013-02-09 13:52:21

flop25
Piwigo Team
2006-07-06
7037

Re: A Method To View Private Photos in WordPress using PiwigoPress


To get a better help : Politeness like Hello-A link-Your past actions precisely described
Check my extensions : more than 30 available
who I am and what I do : http://fr.gravatar.com/flop25
My gallery : an illustration of how to integrate Piwigo in your website

Offline

 

#4 2013-02-13 04:47:41

kenl
Member
2013-01-12
91

Re: A Method To View Private Photos in WordPress using PiwigoPress

I placed the code in the file piwigopress.php.  You may need some additional code to make the whole thing work in your implementation.

Offline

 

#5 2013-03-15 13:04:20

schmimat
Member
Bayern
2013-03-11
6

Re: A Method To View Private Photos in WordPress using PiwigoPress

Hallo,

I have the same installation. (piwigo with no guest login) and I want to try our solution.

But it doesn't work.

First I think there is a ' missing befor ) in the following line.

Code:

curl_setopt($Curl_Session, CURLOPT_URL, '$piwigo_url . "/ws.php?method=pwg.images.getInfo& format=php&image_id=" . $id);

Do you replace some code or do you put it at the end of the file ?
Where is the parameter $username and $password defined ?

Do I need the same login and pwd in WP and Piwigo ?

flop25 wrote:

I think that would be there http://piwigo.org/dev/browser/extension … et.php#L62

Is it possible in PiwigoPress_get.php to get a 4 solution?

Many thanks for the greate work

mfG Matthias

Offline

 

#6 2013-03-15 21:51:12

kenl
Member
2013-01-12
91

Re: A Method To View Private Photos in WordPress using PiwigoPress

Hello,

I will spend a few minutes and clean up the code.  I will then post it and show and example.

KenL

schmimat wrote:

Hallo,

I have the same installation. (piwigo with no guest login) and I want to try our solution.

But it doesn't work.

First I think there is a ' missing befor ) in the following line.

Code:

curl_setopt($Curl_Session, CURLOPT_URL, '$piwigo_url . "/ws.php?method=pwg.images.getInfo& format=php&image_id=" . $id);

Do you replace some code or do you put it at the end of the file ?
Where is the parameter $username and $password defined ?

Do I need the same login and pwd in WP and Piwigo ?

flop25 wrote:

I think that would be there http://piwigo.org/dev/browser/extension … et.php#L62

Is it possible in PiwigoPress_get.php to get a 4 solution?

Many thanks for the greate work

mfG Matthias

Offline

 

#7 2013-03-15 22:25:59

kenl
Member
2013-01-12
91

Re: A Method To View Private Photos in WordPress using PiwigoPress

Hello,

Ok, so here is how I did it.  I am not happy with the code and it is not the best way to implement it.  But, for me, it works.

First, here's an example shortcode from a wordpress post:

Code:

[PiwigoPressPrivate id=[photo id]
 url='http://[piwigo user name]:[piwigo password]@[piwigo url]' 
size='xs' desc=1 class='img-shadow']

Fill in [photo id], [piwigo user name], [piwigo password], and [piwigo url]  For example:

Code:

[PiwigoPressPrivate id=45655 url='http://admin_user:password@myurl.com/piwigo/' size='xs' desc=1 class='img-shadow']

Now, I only changed the file, piwigopress.php.  Once again, not the right way to do it.  I did not change pwg_get_contents() at all.  Changing pwg_get_contents would be a better solution.  Perhaps someone could do it and post a cleaner solution.

So, here's the modified piwigopress.php:

Code:

<?php
/*
Plugin Name: PiwigoPress
Plugin URI: http://wordpress.org/extend/plugins/piwigopress/
Description: PiwigoPress from any open API Piwigo gallery, swiftly includes your photos in Posts/Pages and/or add randomized thumbnails and menus in your sidebar.
Version: 2.22
Author: vpiwigo ( for The Piwigo Team )
Author URI: http://www.vdigital.org/sharing/
*/
if (defined('PHPWG_ROOT_PATH')) return; /* Avoid Automatic install under Piwigo */
/*  Copyright 2009-2012  VDigital  (email : vpiwigo[at]gmail[dot]com)

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/
if (!defined('PWGP_NAME')) define('PWGP_NAME','PiwigoPress');
if (!defined('PWGP_VERSION')) define('PWGP_VERSION','2.2.2');

load_plugin_textdomain('pwg', 'wp-content/plugins/piwigopress', 'piwigopress' );
add_shortcode('PiwigoPress', 'PiwigoPress_photoblog');
add_shortcode('PiwigoPressPrivate', 'PiwigoPress_photoblogPrivate');

function PiwigoPress_photoblog($parm) {
  $default = array(
        'url' => '',
        'id' => 0,     // image_id
        'size' => 'la', // Default large
        'desc' => 0,   // Generate picture description
        'class' => '',  // Specific class
        'style' => '',  // Specific style
        'lnktype' => 'picture' // Default generated link 
    );
  $parm = array_change_key_case( $parm );
  extract( shortcode_atts( $default, $parm ) );
  $previous_url = get_option( 'PiwigoPress_previous_url' );
  //echo "hello" + "<br>";
  if ($previous_url === false) {
    $previous_url = $_SERVER['HTTP_HOST'] . '/piwigo';
    add_option( 'PiwigoPress_previous_url', $previous_url );
  }
  if ($url == '') $url = $previous_url;
  if (substr($url,0,4)!='http') $url = 'http://' . $url;
  if (substr($url,-1)!='/') $url .= '/';
  if ($previous_url != $url) update_option( 'PiwigoPress_previous_url', $url );
  //echo $url . "<br>";
  if ( !is_numeric($id) or $id < 1) {
    return "<!-- PiwigoPress 'id' attribute in error -->\n\n<br>" 
    . __('PiwigoPress shortcode attribute "id" is required and must be positive.','pwg') . "<br>\n\n" ;
  }
  $deriv = array ( 'sq' => 'square', 'th' => 'thumb', 'sm' => 'small', 'xs' => 'xsmall', '2s' => '2small', 
           'me' => 'medium', 'la' => 'large', 'xl' => 'xlarge', 'xx' => 'xxlarge');
  if (!function_exists('pwg_get_contents')) include 'PiwigoPress_get.php';
  $response = pwg_get_contents( $url . 'ws.php?method=pwg.images.getInfo&format=php&image_id=' . $id);
  $thumbc = unserialize($response);
  //echo "response: " . $response . "<br>";
  if ($thumbc["stat"] == 'ok') {
    $picture = $thumbc["result"];
    //var_dump($picture);
    if (isset($picture['derivatives']['square']['url'])) {
      
      $picture['tn_url'] = $picture['derivatives'][$deriv[$size]]['url'] ;
      $atag = '<a title="' . htmlspecialchars($picture['name']) . '" href="' 
        . $url . 'picture.php?/' . $picture['id'] . '" target="_blank">';
      if ( $lnktype == 'none' ) $atag = '';
      if ( $lnktype == 'album' ) {
        $cats = array_reverse($picture['categories']);
        $atag = '<a title="' . htmlspecialchars($cats[0]['name']) . '" href="' 
        . $url . 'index.php?/category/' . $cats[0]['id'] . '" target="_blank">';
      }
      $div = '<div class="PWGP_shortcode ' . $class . '">' . $atag. '<img  class="PWGP_photo" src="' . $picture['tn_url'] . '" alt=""/>';
      if (isset( $picture['comment'] ) and $desc) { 
        $picture['comment'] = stripslashes(htmlspecialchars(strip_tags($picture['comment'])));
        $div .= '<blockquote class="PWGP_caption">' . $picture['comment'] . '</blockquote>'; 
      }
      if ( $lnktype != 'none' ) $div .= '</a>';
      $div .= "\n
      </div>";
    }
  }
  if ($style!='') $style = ' style="'.$style.'"';
  $str =  '<div id="Photo-' . $id . '-'. $size .'" class="PiwigoPress_photoblog"' . $style . '><!-- PiwigoPress Started -->';
  $str .=  $div . '</div><!-- PiwigoPress Ended -->';
  return $str;
}

//********************************************************************************
//********************************************************************************

function PiwigoPress_photoblogPrivate($parm) {
  $default = array(
        'url' => '',
        'id' => 0,     // image_id
        'size' => 'la', // Default large
        'desc' => 0,   // Generate picture description
        'class' => '',  // Specific class
        'style' => '',  // Specific style
        'lnktype' => 'picture' // Default generated link 
    );


  $parm = array_change_key_case( $parm );
  extract( shortcode_atts( $default, $parm ) );
  $previous_url = get_option( 'PiwigoPress_previous_url' );
  //echo "hello" + "<br>";
  if ($previous_url === false) {
    $previous_url = $_SERVER['HTTP_HOST'] . '/piwigo';
    add_option( 'PiwigoPress_previous_url', $previous_url );
  }
  if ($url == '') $url = $previous_url;
  if (substr($url,0,4)!='http') $url = 'http://' . $url;
  if (substr($url,-1)!='/') $url .= '/';
  if ($previous_url != $url) update_option( 'PiwigoPress_previous_url', $url );
  //echo $url . "<br>";
  if ( !is_numeric($id) or $id < 1) {
    return "<!-- PiwigoPress 'id' attribute in error -->\n\n<br>" 
    . __('PiwigoPress shortcode attribute "id" is required and must be positive.','pwg') . "<br>\n\n" ;
  }
  $deriv = array ( 'sq' => 'square', 'th' => 'thumb', 'sm' => 'small', 'xs' => 'xsmall', '2s' => '2small', 
           'me' => 'medium', 'la' => 'large', 'xl' => 'xlarge', 'xx' => 'xxlarge');

  if (!function_exists('pwg_get_contents')) include 'PiwigoPress_get.php';
  
  session_write_close();
  
  $Curl_Session = curl_init($url . 'ws.php?method=pwg.session.login');
  $tmp_fname = tempnam("/tmp", "COOKIE");
  curl_setopt ($Curl_Session, CURLOPT_COOKIEJAR, $tmp_fname);
  curl_setopt ($Curl_Session, CURLOPT_POST, 1);
  curl_setopt ($Curl_Session, CURLOPT_POSTFIELDS, "username=admin&password=Byse90100");
  curl_setopt ($Curl_Session, CURLOPT_FOLLOWLOCATION, 1);
  curl_setopt ($Curl_Session, CURLOPT_COOKIEFILE,  $tmp_fname);
  curl_setopt($Curl_Session, CURLOPT_RETURNTRANSFER, 1);
  $result = curl_exec ($Curl_Session);
  
    //$ch = @curl_init();

    $Curl_Session = curl_init();
    curl_setopt ($Curl_Session, CURLOPT_COOKIEJAR, $tmp_fname);
    curl_setopt ($Curl_Session, CURLOPT_COOKIEFILE,  $tmp_fname);
    curl_setopt($Curl_Session, CURLOPT_TIMEOUT, 5);
    curl_setopt ($Curl_Session, CURLOPT_POST, 0);
    curl_setopt($Curl_Session, CURLOPT_URL, $url . 'ws.php?method=pwg.session.getStatus');
  //  curl_setopt($Curl_Session, CURLOPT_HEADER, 1);
    curl_setopt($Curl_Session, CURLOPT_USERAGENT, 'Mozilla/5.0');
    curl_setopt($Curl_Session, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($Curl_Session, CURLOPT_FOLLOWLOCATION, 1);    
    curl_setopt ($Curl_Session, CURLOPT_SSL_VERIFYHOST, 0);
    curl_setopt ($Curl_Session, CURLOPT_SSL_VERIFYPEER, 0); 

  //  $value = @curl_exec($Curl_Session);
  //  $status = @curl_getinfo($Curl_Session);  
    //echo "<br>login result " . $value . "<br>";
  
    curl_setopt($Curl_Session, CURLOPT_URL, $url . 'ws.php?method=pwg.images.getInfo&format=php&image_id=' . $id);
    $response = @curl_exec($Curl_Session);
    $status = @curl_getinfo($Curl_Session);  
    //echo "<br>get info result " . $response . "<br>";

    @curl_close($Curl_Session);
          
  //$response = pwg_get_contents( $url . 'ws.php?method=pwg.images.getInfo&format=php&image_id=' . $id);
  $thumbc = unserialize($response);
  
  //echo "<br>new thumbc: " . $thumbc["stat"] . "<br>";
  
  if ($thumbc["stat"] == 'ok') {
    $picture = $thumbc["result"];
    //var_dump($picture);
    if (isset($picture['derivatives']['square']['url'])) {
      
      $picture['tn_url'] = $picture['derivatives'][$deriv[$size]]['url'] ;
      //$atag = '<a title="' . htmlspecialchars($picture['name']) . '" href="' 
      //  . $url . 'picture.php?/' . $picture['id'] . '" target="_blank">';
      $atag = "";
      if ( $lnktype == 'none' ) $atag = '';
      if ( $lnktype == 'album' ) {
        $cats = array_reverse($picture['categories']);
        $atag = '<a title="' . htmlspecialchars($cats[0]['name']) . '" href="' 
        . $url . 'index.php?/category/' . $cats[0]['id'] . '" target="_blank">';
      }
      $div = '<div class="PWGP_shortcode ' . $class . '">' . $atag. '<img  class="PWGP_photo" src="' . $picture['tn_url'] . '" alt=""/>';
      if (isset( $picture['comment'] ) and $desc) { 
        $picture['comment'] = stripslashes(htmlspecialchars(strip_tags($picture['comment'])));
        $div .= '<blockquote class="PWGP_caption">' . $picture['comment'] . '</blockquote>'; 
      }
      if ( $lnktype != 'none' ) $div .= '</a>';
      $div .= "\n
      </div>";
    }
  }
  if ($style!='') $style = ' style="'.$style.'"';
  $str =  '<div id="Photo-' . $id . '-'. $size .'" class="PiwigoPress_photoblog"' . $style . '><!-- PiwigoPress Started -->';
  $str .=  $div . '</div><!-- PiwigoPress Ended -->';
  return $str;
}


class PiwigoPress extends WP_Widget
{
  function PiwigoPress(){
    $widget_ops = array('classname' => PWGP_NAME,
      'description' => __( "Adds a thumbnail and its link (to the picture) inside your blog sidebar.",'pwg') );
    $control_ops = array('width' => 780, 'height' => 300);
    $this->WP_Widget(PWGP_NAME, PWGP_NAME, $widget_ops, $control_ops);
  }
  // Code generator
  function widget($args, $gallery){
    include 'PiwigoPress_code.php';
  }
  function update($new_gallery, $old_gallery){
    $gallery = $old_gallery;
    $gallery['title'] = strip_tags(stripslashes($new_gallery['title']));
    $gallery['thumbnail'] = (bool) $new_gallery['thumbnail'];
    $gallery['thumbnail_size'] = strip_tags(stripslashes($new_gallery['thumbnail_size']));
    $gallery['format'] = strip_tags(stripslashes($new_gallery['format']));
    $gallery['piwigo'] = strip_tags(stripslashes($new_gallery['piwigo']));
    $gallery['external'] = strip_tags(stripslashes($new_gallery['external']));
    $gallery['number'] = intval(strip_tags(stripslashes($new_gallery['number'])));
    $gallery['category'] = intval(strip_tags(stripslashes($new_gallery['category'])));
    $gallery['from'] = intval(strip_tags(stripslashes($new_gallery['from'])));
    $gallery['divclass'] = strip_tags(stripslashes($new_gallery['divclass']));
    $gallery['class'] = strip_tags(stripslashes($new_gallery['class']));
    $gallery['most_visited'] = (strip_tags(stripslashes($new_gallery['most_visited'])) == 'true') ? 'true':'false';
    $gallery['best_rated'] = (strip_tags(stripslashes($new_gallery['best_rated'])) == 'true') ? 'true':'false';
    $gallery['most_commented'] = (strip_tags(stripslashes($new_gallery['most_commented'])) == 'true') ? 'true':'false';
    $gallery['random'] = (strip_tags(stripslashes($new_gallery['random'])) == 'true') ? 'true':'false';
    $gallery['recent_pics'] = (strip_tags(stripslashes($new_gallery['recent_pics'])) == 'true') ? 'true':'false';
    $gallery['calendar'] = (strip_tags(stripslashes($new_gallery['calendar'])) == 'true') ? 'true':'false';
    $gallery['tags'] = (strip_tags(stripslashes($new_gallery['tags'])) == 'true') ? 'true':'false';
    $gallery['comments'] = (strip_tags(stripslashes($new_gallery['comments'])) == 'true') ? 'true':'false';
    $gallery['mbcategories'] = (strip_tags(stripslashes($new_gallery['mbcategories'])) == 'true') ? 'true':'false';
    $gallery['allsel'] = (strip_tags(stripslashes($new_gallery['allsel'])) == 'true') ? 'true':'false';
    $gallery['filter'] = (strip_tags(stripslashes($new_gallery['filter'])) == 'true') ? 'true':'false';
    $gallery['lnktype'] = strip_tags(stripslashes($new_gallery['lnktype']));
    if ( current_user_can('unfiltered_html') )
      $gallery['text'] =  $new_gallery['text'];
    else
      $gallery['text'] = stripslashes( wp_filter_post_kses( addslashes($new_gallery['text']) ) ); // wp_filter_post_kses() expects slashed
    return $gallery;
  }
  function form($gallery){
    // Options
    include 'PiwigoPress_options.php';
  }
}

// Register 
function PiwigoPress_Init() {
      register_widget('PiwigoPress');
}
add_action('widgets_init', PWGP_NAME . '_Init');

// Style allocation
function PiwigoPress_load_in_head() {
  if (defined('PWGP_CSS_FILE')) return; // Avoid several links to CSS in case of several usage of PiwigoPress... 
  define('PWGP_CSS_FILE','');
  if ( is_admin() ) {
    echo '<link media="all" type="text/css" href="' . 
      WP_PLUGIN_URL . '/piwigopress/css/piwigopress_adm.min.css?ver=' . PWGP_VERSION . '" id="piwigopress_a-css" rel="stylesheet">'; // that's fine
  }
  else {
    echo '<link media="all" type="text/css" href="' . 
      WP_PLUGIN_URL . '/piwigopress/css/piwigopress.css?ver=' . PWGP_VERSION . '" id="piwigopress_c-css" rel="stylesheet">'; // that's fine
  }
}
add_action('wp_head',  PWGP_NAME . '_load_in_head');

// Script to be used
function PiwigoPress_load_in_footer() {
  /* Scripts */
  wp_register_script( 'piwigopress_s', WP_PLUGIN_URL . '/piwigopress/js/piwigopress.js', array('jquery'), PWGP_VERSION );
  wp_enqueue_script( 'jquery'); // include it even if it's done
  if ( is_admin() ) {
    wp_register_script( 'piwigopress_a', WP_PLUGIN_URL . '/piwigopress/js/piwigopress_adm.min.js', array('jquery'), PWGP_VERSION );
    wp_enqueue_script( 'jquery-ui-draggable' );
    wp_enqueue_script( 'jquery-ui-droppable' );
    wp_enqueue_script( 'piwigopress_a' );
  }
  wp_enqueue_script( 'piwigopress_s' );
}
add_action('wp_footer',  PWGP_NAME . '_load_in_footer');

function PiwigoPress_register_plugin() {
    if (!current_user_can('edit_posts') && !current_user_can('edit_pages'))
        return;
    add_action('admin_head', PWGP_NAME . '_load_in_head');
}

add_action('init', PWGP_NAME . '_register_plugin');

// Admin code only if distributed and in Admin
if ( is_admin() ) {
    @include 'piwigopress_admin.php';
}
function piwigopress_plugin_links($links, $file) {  
  $plugin = plugin_basename(__FILE__);  
  
  if ($file == $plugin) // only for this plugin  
    return array_merge( $links,   
      // array( sprintf( '<a href="options-general.php?page=%s">%s</a>', $plugin, __('Settings') ) ),  
      array( '<a href="http://wordpress.org/extend/plugins/piwigopress/faq/" target="_blank">' . __('FAQ') . '</a>' ),  
      array( '<a href="http://wordpress.org/support/plugin/piwigopress" target="_blank">' . __('PiwigoPress Support') . '</a>' ),  
      array( '<a href="http://piwigo.org/" target="_blank">' . __('Piwigo site') . '</a>' )  
    );  
  return $links;  
}  
  
add_filter( 'plugin_row_meta', PWGP_NAME . '_plugin_links', 10, 2 );  
?>

Good luck! Let me know if it works.
KenL

Offline

 
  •  » Extensions
  •  » A Method To View Private Photos in WordPress using PiwigoPress

Board footer

Powered by FluxBB

github twitter newsletter Donate Piwigo.org © 2002-2024 · Contact