FS-Mod/FSMod/SpecialCleanUserImageCache.php

101 lines
3 KiB
PHP

<?php
/**
* Special:StuBU, a special page that implements a StuBu generator form.
* Based on the pdfforms typo3 plugin of Felix B. Holzke.
*
* @file
* @ingroup Extensions
* @author Björn Kinscher
* @copyright © 2009 Björn Kinscher
* @license GNU General Public Licence 2.0 or later
*/
if( !defined( 'MEDIAWIKI' ) ) {
echo( "not a valid entry point.\n" );
die( 1 );
}
/**
* Provides the stubu form
* @ingroup SpecialPage
*/
class SpecialCleanUserImageCache extends SpecialPage {
/**
* Constructor
*/
public function __construct() {
parent::__construct( 'CleanUserImageCache' , 'fsmod-clean-user-image-cache');
}
/**
* Main execution function
*
* @param $par Mixed: Parameters passed to the page
*/
public function execute( $par ) {
$this->checkPermissions();
global $wgLdapBase, $wgLdapServer, $wgLdapPwd, $wgLdapUser;
$out = $this->getOutput();
$request = $this->getRequest();
$clean = $request->getVal( 'submit' );
$out->setPageTitle( wfMessage( 'fscleanimagecache-title' ) );
$out->addHTML("<h2>Leere Image Cache</h2>");
if ($clean===null){
$out->addHTML("
<form action=\"https://www.fs-infmath.uni-kiel.de/wiki/Spezial:CleanUserImageCache\" method=\"POST\">
<input type=\"submit\" name=\"submit\" value=\"Leere Cache\" />
</form><br />");
}else{
syslog(LOG_INFO, "Cleaning up FSMod image cache.");
$cache_path = dirname(__FILE__).'/cache';
try {
if ( !$ldap_con = ldap_connect($wgLdapServer) )
throw new Exception('Could not connect to ldap server.');
ldap_set_option($ldap_con, LDAP_OPT_PROTOCOL_VERSION, 3);
if ( !$ldap_bd = ldap_bind($ldap_con, $wgLdapUser, $wgLdapPwd) )
throw new Exception('Could not bind to server. Error is ' . ldap_error($ldap_con));
if ( !$result = ldap_search($ldap_con, $wgLdapBase, '(uid=*)') )
throw new Exception('Error in query.');
$data = ldap_get_entries($ldap_con, $result);
$img_crc = Array();
for ($i = 0; $i < $data['count']; $i++) {
if ((isset($data[$i]['jpegphoto'])) &&
($tmp = crc32($data[$i]['jpegphoto'][0])))
$img_crc[] = $tmp . ".jpg";
}
$dh = opendir($cache_path);
while ( false !== ($filename = readdir($dh)) ) {
if ( ($filename == '.') || ($filename == '..') )
continue;
if ( !in_array($filename, $img_crc) )
unlink($cache_path . '/' . $filename);
}
closedir($dh);
}
catch (Exception $e) {
echo 'No connection to database.';
syslog(LOG_ERR, 'FSMod: No connection to database.');
}
if ( $ldap_con )
ldap_close($ldap_con);
$out->addHTML("Veraltete Nutzerbilder wurden entfernt!");
}
}
}