1<?php 2/** 3 * DokuWiki Plugin dlcounter (Action Component) 4 * 5 * records and displays download counts for files with specified extensions in the media library 6 * 7 * @author Phil Ide <phil@pbih.eu> 8 * @license GPL 2 (http://www.gnu.org/licenses/gpl.html) 9 */ 10 11// must be run within Dokuwiki 12if (!defined('DOKU_INC')) { 13 die(); 14} 15 16class action_plugin_dlcounter extends DokuWiki_Action_Plugin 17{ 18 19 /** 20 * Registers a callback function for a given event 21 * 22 * @param Doku_Event_Handler $controller DokuWiki's event controller object 23 * 24 * @return void 25 */ 26 public function register(Doku_Event_Handler $controller) 27 { 28 $controller->register_hook('MEDIA_SENDFILE', 'BEFORE', $this, 'handle_media_sendfile'); 29 30 } 31 32 /** 33 * [Custom event handler which performs action] 34 * 35 * Called for event: 36 * 37 * @param Doku_Event $event event object by reference 38 * @param mixed $param [the parameters passed as fifth argument to register_hook() when this 39 * handler was registered] 40 * 41 * @return void 42 */ 43 public function handle_media_sendfile(Doku_Event $event, $param) 44 { 45 $data = $event->data; 46 $extension = str_replace(' ', '', strtolower($this->getConf('extensions')) ); 47 $extension = explode( ",", $extension ); 48 $ok = true; 49 50 if( in_array( strtolower($data['ext']), $extension ) ){ 51 $path = DOKU_INC."/data/counts"; 52 if( !file_exists($path) ){ 53 $ok = mkdir($path,0755); 54 } 55 if( $ok ){ 56 $fname = $path.'/download_counts.json'; 57 $json = array(); 58 if( file_exists( $fname ) ){ 59 $json = json_decode( file_get_contents($fname), TRUE ); 60 } 61 62 $count = 0; 63 if( array_key_exists($data['media'], $json) ){ 64 $count = $json[$data['media']]; 65 } 66 $count++; 67 $json[$data['media']] = $count; 68 69 file_put_contents( $fname, json_encode($json) ); 70 } 71 } 72 } 73 74} 75 76