Problems, need help? Have a tip or advice? Post it here.
13 posts Page 2 of 2
Hello, i want to make Google Link Grabber with couch but i have some problem to generate couch tag with native php.
here is my code:

Grabber Code:
Code: Select all
<?php
class DriveGrabber {
   // Variables
   public $cacheDir = __DIR__ .'/drive_cache/';
   public $cacheLen = 60 * 60; // 60 Minutes
   
   // Get download link
   function getDownloadLink($fileId) {
      $cacheFile   = $this->cacheDir . md5($fileId) . ".cache";
      $returnUrl   = null;
      $driveUrl   = "https://drive.google.com/uc?id=".urlencode($fileId)."&export=download";
      
      if (file_exists($cacheFile)) {
         $resource = file_get_contents($cacheFile);
         $resource = explode('~', $resource);
         
         if (is_array($resource) && isset($resource[1]) && (time() - $resource[0]) <= 3600) {
            $returnUrl = trim($resource[1]);
         }
      }
      
      if ($returnUrl == null) {
         $returnUrl = $this->parseUrl($driveUrl);
         $this->cacheLink($cacheFile, $returnUrl);
      }
      
      return $returnUrl;
   }
   
   function cacheLink($path, $link) {
      // Create cache directory
      if (!file_exists($this->cacheDir)) {
         mkdir($this->cacheDir, 0777, true);
      }
      
      // Create cache file
      $data = time().'~'.$link;
      file_put_contents($path, $data);
   }
   
   // Search for download link from drive url
   function parseUrl($url, $cookies = null) {
      $fileId = null;
      $idPos = strpos($url, 'id=');
      
      if ($idPos !== false) {
         $fileId = substr($url, $idPos+3);
         $fileId = substr($fileId, 0, strpos($fileId, '&'));
      }
      
      $ch = curl_init();
      curl_setopt($ch, CURLOPT_COOKIESESSION, true);
      curl_setopt($ch, CURLOPT_URL, $url);
      curl_setopt($ch, CURLOPT_HEADER, true);
      curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
      
      if ($cookies != null && is_array($cookies) && count($cookies) > 0) {
         curl_setopt($ch, CURLOPT_COOKIE, implode('; ', $cookies));
      }
      
      $response = curl_exec($ch);
      
      $headers = substr($response, 0, curl_getinfo($ch, CURLINFO_HEADER_SIZE));
      $headers = explode("\r\n", $headers);
      
      $redirect = null;
      $cookies = array();
      
      foreach ($headers as $header) {
         $delimeterPos = strpos($header, ':');
         if ($delimeterPos === false)
            continue;
         
         $key = trim(strtolower(substr($header, 0, $delimeterPos)));
         $value   = trim(substr($header, $delimeterPos+1));
         
         if ($key == 'location') {
            $redirect = $value;
         }
         
         if (strpos($key, 'cookie') !== false) {
            $cookies[] = substr($value, 0, strpos($value, ';'));
         }
      }
      
      if ($redirect == null) {
         $confirm = strpos($response, "confirm=");
         
         if ($confirm !== false) {
            $confirm = substr($response, $confirm, strpos($response, '"'));
            $confirm = substr($confirm, strpos($confirm, '=')+1);
            $confirm = substr($confirm, 0, strpos($confirm, '&'));
            
            $redirect = $this->parseUrl("https://drive.google.com/uc?export=download&confirm=".urlencode($confirm)."&id=".urlencode($fileId), $cookies);
         }
      }
      
      return $redirect;
   }
}
?>
I also have put code above into Couch Addon and set it to kfunctions.php it was works perfectly.

Code: Select all
<?php
if ( !defined('K_COUCH_DIR') ) die(); // cannot be loaded directly

//require_once( K_COUCH_DIR.'addons/cart/cart.php' );
//require_once( K_COUCH_DIR.'addons/inline/inline.php' );
//require_once( K_COUCH_DIR.'addons/extended/extended-folders.php' );
//require_once( K_COUCH_DIR.'addons/extended/extended-comments.php' );
//require_once( K_COUCH_DIR.'addons/extended/extended-users.php' );
//require_once( K_COUCH_DIR.'addons/routes/routes.php' );
//require_once( K_COUCH_DIR.'addons/jcropthumb/jcropthumb.php' );
//require_once( K_COUCH_DIR.'addons/bootstrap-grid/bootstrap-grid.php' );
require_once( K_COUCH_DIR.'addons/drive-grabber/drive-grabber.php' );

And here is the movie.php code:
Code: Select all
<?php require_once( '../admin/cms.php' ); ?>
<cms:template title='Player' clonable='1'>
<cms:editable name='movie_link' label='Movie Link' required='1' type='text' />
</cms:template>

<?php
$drive = new DriveGrabber();
$videoUrl = $drive->getDownloadLink('1otiVNHxYs5wJH7OMDedhXv3bL3jtC3LQ');
?>

<!doctype html>
<html>
   <head>
      <title>Player | Movie123</title>
      
      <style>
         html{
            padding: 0px;
            margin: 0px
         }
         
         body{
            margin: 0px;
            padding: 0px;
            background:black
         }
      </style>
      
      <script src="https://content.jwplatform.com/libraries/smC1HW9J.js"></script>
    <script>jwplayer.key="BiSnaPijub75RBQ8wmE/EHROZeNTH1neTb9gLR1QIooNVALLwBoi2w==";</script>
    <!---<script src="<cms:show k_site_link/>assets/themes/moview/js/jwbox.js"></script>--->
   </head>

   <body>

   <div style="margin: 0px;padding: 0px" id="myElement"></div>

   <script>
   var playerInstance = jwplayer("myElement")
   playerInstance.setup({
   file: "<?php echo $videoUrl ?>",
   type: "mp4",
   primary: "flash",
   provider: "video",
   image: "<cms:show movie_wallpaper/>",
   width: "100%",
   height: "100%",
   skin: {
   name: "vapor",
   active: "red",
   inactive: "grey",
   background: "white"
   },

   tracks: [{
   <cms:show_repeatable 'repeat_movie_subtitle'>
            file: "<cms:show movie_subtitle/>",
            label: "<cms:show subtitle_country/>",
            kind: "captions",
            "default": <cms:show subtitle_default/>
    },{
    </cms:show_repeatable>
    }],
   responsive: true
   }).on('play', function () {
   statusCollection('play');
   playerData();
   }).on('pause', function () {
   statusCollection('pause');
   playerData();
   }).on('complete', function () {
   this.setControls(true);
   statusCollection('complete');
   });
   </script>

</body>
</html>
<?php COUCH::invoke(); ?>

When i tried with actual video id 1otiVNHxYs5wJH7OMDedhXv3bL3jtC3LQ from google video it was work.
Here the works code:
Code: Select all
<?php
$drive = new DriveGrabber();
$videoUrl = $drive->getDownloadLink('1otiVNHxYs5wJH7OMDedhXv3bL3jtC3LQ');
?>

Now, my problem is when i set the code to couch <cms:show movie_link/> tag it's not executing. Need some help here.
Here is the error code:
Code: Select all
<?php
$drive = new DriveGrabber();
$videoUrl = $drive->getDownloadLink('<cms:show movie_link/>');
?>

I've tried with <cms:php>, not work either.
Here is the error code:
Code: Select all
<cms:php>
$drive = new DriveGrabber();
$videoUrl = $drive->getDownloadLink('<cms:show movie_link/>');
</cms:php>

I've tried wrapping it with <cms:pages masterpage='movie.php'></cms:pages> also, but still no luck.
Here is the error code:
Code: Select all
<cms:pages masterpage='movie.php'>
<cms:php>
$drive = new DriveGrabber();
$videoUrl = $drive->getDownloadLink('<cms:show movie_link/>');
</cms:php>
</cms:pages>

Need to find what is my problem, thankyou.
@setiawanfarlin,

The problem can be understood if you keep in mind the sequence in which raw PHP and Couch tags execute (please see following post in this very thread - viewtopic.php?f=4&t=7594#p11751).

OK, to make things manageable, I'll redact your code to only the two parts that are essential.
Code: Select all
<?php
    $drive = new DriveGrabber();
    $videoUrl = $drive->getDownloadLink('1otiVNHxYs5wJH7OMDedhXv3bL3jtC3LQ');
?>

..

<body>
    <script>
       playerInstance.setup({
           file: "<?php echo $videoUrl ?>",
           type: "mp4"}
       )
    </script>
</body>

So basically we set a variable named $videoUrl in the first part above and then use that variable in the second part.

The version above is the only method that is working for you. In it we are using raw PHP for both the parts.
Let us see what happens in your first try to use Couch tags instead -
Code: Select all
<?php
$drive = new DriveGrabber();
$videoUrl = $drive->getDownloadLink('<cms:show movie_link/>');
?>

Assuming we are on a page-view (as your template is clonable and so 'movie_link' couch variable will be available only while visiting a page), in the code above you are using Couch tags within raw PHP.

As explained in the post about sequence, raw PHP gets the first shot at executing. So when the $drive->getDownloadLink('<cms:show movie_link/>'); PHP statement runs, Couch would not even be in the scene. So, you would actually be passing a literal string <cms:show movie_link/> as the video ID. Obviously this will fail.

Ok, your next try -
Code: Select all
<cms:php>
$drive = new DriveGrabber();
$videoUrl = $drive->getDownloadLink('<cms:show movie_link/>');
</cms:php>

This is better as now you are running your code through Couch <cms:php> so we can be sure that the <cms:show movie_link/> will output the content of your field.

The code above should set the $videoUrl variable.
But now you need to be careful about one thing - setting PHP variables set within <cms:php> is like setting variables within a PHP function i.e. they have a local scope and once the block ends the variables are lost.

So, when you try to use that variable in the second part of your code, it won't be there.
The trick is to use 'global' variables - modify your block to make it as follows
Code: Select all
<cms:php>
global $videoUrl;

$drive = new DriveGrabber();
$videoUrl = $drive->getDownloadLink('<cms:show movie_link/>');
</cms:php>

As you can see, we have declared $videoUrl as global so now we can access it from any other part of the template.
If you need more info on this global stuff please see this post -
viewtopic.php?f=4&t=10627#p26647

OK, finally the second part of your code where you want to use that variable.
Your existing code is -
Code: Select all
playerInstance.setup({
   file: "<?php echo $videoUrl ?>",
   type: "mp4"}
)

You are using raw PHP to access that variable.
I think you should now be able to see clearly that raw PHP would have executed much before the Couch <cms:php> tag we used in the first part would run. So, the code above is useless.

We need to use Couch cms:php for this part too.
Change the code to make it as follows -
Code: Select all
playerInstance.setup({
   file: "<cms:php> global $videoUrl; echo $videoUrl; </cms:php>",
   type: "mp4"}
)

Please note the use of 'global' above without which the variable wouldn't be found.

Please try and let me know if this helps.
If you feel like, also take a look at another post on using PHP with Couch -
viewtopic.php?f=2&t=9882#p22669

Hope this helps.
Wow, works like a charms KK.

I know my what is wrong with my code right now.
Actually, I've tried the global $videoUrl; before, but i forgot to wrap it with <cms:php></cms:php> at the javascript code.

Thankyou so much KK, it's help me so much.
Have a nice day KK.
13 posts Page 2 of 2