1<?php
2/* author: alice@muc.ccc.de
3 */
4if (!defined('DOKU_INC')) die();
5class helper_plugin_podcast extends DokuWiki_Plugin {
6  function get_info( $page ) {
7            $dthlp =& plugin_load('helper', 'data');
8    $sqlite = $dthlp->_getDB();
9    if(!$sqlite) return false;
10    $res = $sqlite->query( 'SELECT
11	T1.key as key,
12	T1.value as value
13      FROM pages
14	LEFT JOIN data AS T1 ON T1.pid = pages.pid
15	WHERE page = ?', $page );
16    $rows = $sqlite->res2arr($res);
17    $cnt = count($rows);
18    $p = array( );
19    if( $cnt ) {
20      foreach( $rows as $i => $d ) {
21        if( isset( $p[$d['key']] )) { $p[$d['key']].= ', '.$d['value']; }
22        else { $p[$d['key']] = $d['value']; }}}
23    if( !$p['nr'] ) {
24      $path = explode( ':', $page );
25      $p['nr'] = array_pop( $path ); }
26
27    $files = $this->get_source_files( $p['nr'] );
28
29    $p['files'] = $files;
30    return $p;
31  }
32  function get_source_files( $name ) {
33    $name = $this->getConf( 'podcast_prefix' ).$name;
34    $extensions = explode( ',', $this->getConf( 'podcast_extensions' ));
35    $files = array( );
36
37    // check page metadata for cached sources
38    // actly no idea what i m doing here
39    $meta = p_get_metadata( $page, 'source', METADATA_RENDER_USING_CACHE );
40    foreach( $extensions as $ext ) {
41      $f = "$name.$ext";
42      if( isset( $meta[$ext] ) && $meta[$ext]['url'] == $f ) {
43        $files[$ext] = $meta[$ext]; }
44      else {
45        $s = $this->get_headers_length( $f );
46        $files[$ext] = array(
47            'url' => $f,
48            'size' => $s,
49            'hsize' => $this->gethumanfilesize( $s, 0 )); }}
50    p_set_metadata( $page, array( 'source' => $files ), false, false );
51    return $files;
52  }
53  function get_headers_length($url) {
54    $url_info=parse_url($url);
55    $port = isset($url_info['port']) ? $url_info['port'] : 80;
56    $fp=fsockopen($url_info['host'], $port, $errno, $errstr, 10 );
57    if($fp) {
58        $head = "GET ".@$url_info['path']."?".@$url_info['query']." HTTP/1.0\r\n";
59        if (!empty($url_info['port'])) {
60            $head .= "Host: ".@$url_info['host'].":".$url_info['port']."\r\n"; }
61	else {
62            $head .= "Host: ".@$url_info['host']."\r\n"; }
63        $head .= "Connection: Close\r\n";
64        $head .= "Accept: */*\r\n";
65        $head .= $refererline;
66        $head .= $authline;
67        $head .= "\r\n";
68        fputs($fp, $head);
69        while( !feof( $fp ) or ( $eoh == true )) {
70            if( $v = fgets( $fp, 1024 )) {
71                if( $v == "\r\n" ) {
72                    $eoh = true;
73                    break;
74                } else {
75        	  if( preg_match( '/Content-Length: ([0-9]*)/', $v, $m )) {
76		    $length = $m[1]; }
77		  elseif( preg_match( '/Location: (http:[^ ]*)/', $v, $m )) {
78		    $location = $m[1]; }
79		  elseif( preg_match( '/HTTP\/1\.1 ([0-9]*)/', $v, $m )) {
80		    $status = $m[1]; }}} }}
81    if( $status === '302' ) return $this->get_headers_length( trim( $location ));
82    if( $status === '200' ) return $length;
83    return false;
84  }
85  function gethumanfilesize( $bytes, $decimals = 2 ) {
86    $sz = 'BKMGTP';
87    $factor = floor((strlen($bytes) - 1) / 3);
88    return sprintf("%.{$decimals}f", $bytes / pow(1024, $factor)) . @$sz[$factor];
89  }
90  function getfiles( $name ) {
91    $name = $this->getConf( 'podcast_prefix' ).$name;
92    $extensions = explode( ',', $this->getConf( 'podcast_extensions' ));
93    $files = array( );
94
95    foreach( $extensions as $ext ) {
96      $f = "$name.$ext";
97      $s = $this->get_headers_length( $f );
98      if( !$s ) continue;
99      $files[$ext] = array(
100          'url' => $f,
101          'size' => $s,
102          'hsize' => $this->gethumanfilesize( $s, 0 )); }
103    return $files;
104  }
105}
106