Beee

Vion Cocoon

Sep 4th, 2019
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.26 KB | None | 0 0
  1. <?php
  2.     /*
  3.     Plugin Name: Vion : Cocoon
  4.     Version: 1.0.0
  5.     Description: Retrieves images from Cocoon media
  6.     Author: M. Dekker
  7.     Author URI: http://www.totaldesign.com
  8.     Text-domain: vion-cocoon
  9.     */
  10.    
  11.     if ( ! defined( 'ABSPATH' ) ) {
  12.         exit;
  13.     } // Exit if accessed directly
  14.    
  15.     if ( ! class_exists( 'VionCocoon' ) ) {
  16.        
  17.         class VionCocoon {
  18.            
  19.             var $wsdl;
  20.             var $bb;
  21.             var $user;  // Provided by Cocoon
  22.             var $sk;    // Provided by Cocoon
  23.            
  24.             var $filters = array();
  25.            
  26.             function __construct() {
  27.    
  28.                 $this->wsdl     = getenv( 'COCOON_API_WSDL' );
  29.                 $this->bb       = getenv( 'COCOON_API_DOMAIN' );
  30.                 $this->user     = getenv( 'COCOON_API_USERNAME' );
  31.                 $this->sk       = getenv( 'COCOON_API_SECRET' );
  32.                 $this->settings = array(
  33.                     'path'    => trailingslashit( dirname( __FILE__ ) ),
  34.                     'version' => '1.0.0',
  35.                 );
  36.    
  37.                 try {
  38.                     $this->authenticate();
  39.                 } catch ( SoapFault $e ) {
  40.                     // throw error ?
  41.                 }
  42.    
  43.             }
  44.    
  45.             /**
  46.              * @throws SoapFault
  47.              */
  48.             function authenticate() {
  49.                 $requestId = (string)microtime( true ); //Something unique
  50.                
  51.                 // create hash
  52.                 $hash = $this->get_request_hash( $requestId );
  53.                
  54.                 $oAuth            = new stdClass;
  55.                 $oAuth->username  = $this->user;
  56.                 $oAuth->requestId = $requestId;
  57.                 $oAuth->hash      = $hash;
  58.                
  59.                 //Init client
  60.                 $soap_connection = new SoapClient( $this->wsdl );
  61.                 $soap_connection->__setSoapHeaders( new SoapHeader( 'auth', 'authenticate', $oAuth ) );
  62.  
  63.                 try {
  64.                     return $soap_connection;
  65.                 }
  66.                 catch ( SoapFault $oSoapFault ) {
  67.                     error_log( $oSoapFault->faultcode, $oSoapFault->getMessage() );
  68.                     // var_dump( $oSoapFault->faultcode, $oSoapFault->getMessage() );
  69.                    
  70.                 }
  71.                
  72.             }
  73.    
  74.             /**
  75.              * Hash a string
  76.              *
  77.              * @param $request_id
  78.              *
  79.              * @return string
  80.              */
  81.             function get_request_hash( $request_id ) {
  82.                 $concatString = $this->bb . $this->user . $request_id . $this->sk;
  83.                 $hash         = sha1( $concatString );
  84.                
  85.                 return $hash;
  86.             }
  87.    
  88.            
  89.             /**
  90.              * Get image url from Cocoon media lib
  91.              *
  92.              * @param bool $image_id
  93.              *
  94.              * @return bool|string
  95.              */
  96.             function get_image( $image_id, $size ) {
  97.    
  98.                 $soap       = $this->authenticate();
  99.                 $file_name  = false;
  100.                 $image_info = $soap->getFile( $image_id );
  101.                 $image_path = 'files/' . $size . '/';
  102.                
  103.                 if ( is_array( $image_info ) && isset( $image_info[ 'filename' ] ) ) {
  104.                     $file_name = ( is_array( $image_info ) && isset( $image_info[ 'filename' ] ) ) ? $image_info[ 'filename' ] : false;
  105.                 }
  106.    
  107.                 $image_url = 'https://vionfoodgroup.use-cocoon.com/' . $image_path . $file_name . '.' . $image_info[ 'extension' ];
  108.                
  109.                 return $image_url;
  110.             }
  111.            
  112.             /**
  113.              * return the one true VionCocoon instance
  114.              *
  115.              * @return VionCocoon
  116.              */
  117.             public static function get_instance() {
  118.                 static $instance;
  119.                
  120.                 if ( $instance === null ) {
  121.                     $instance = new self();
  122.                 }
  123.                
  124.                 return $instance;
  125.             }
  126.            
  127.         }
  128.        
  129.         // initialize
  130.         VionCocoon::get_instance();
  131.        
  132.     }
Add Comment
Please, Sign In to add comment