_initTable(); } public function setShotDir( $dir ) { if( !is_dir( $dir ) || !is_writable( $dir ) ) { throw new Exception( $dir . ' is no directory or not writable!' ); } $this->_shotDir = $dir; return true; } protected function _initTable() { WB::loadClass( 'WBTable' ); $this->_table = new WBTable(); $con = array( 'host' => 'localhost', 'database' => 'screenshot', 'user' => 'gerd', 'password' => 'gerd123' ); $this->_table->connect( $con ); $tables = array( 'screen' => array( 'table' => 'screen', 'primary' => 'sid', 'created' => 'created' ), 'queue' => array( 'table' => 'queue', 'primary' => 'sid' ) ); $this->_table->setTables( $tables ); return true; } public function add( $url ) { if( empty( $url ) ) { return null; } $clause = array(); $clause[] = array( 'field' => 'url', 'value' => $url ); $ids = $this->_table->getIds( 'screen', $clause ); if( count( $ids ) ) { return $ids[0]; } $contextOptions = array( 'http' => array( //'proxy' => 'tcp://proxy.nero-de.internal:3128', //'request_fulluri' => true ) ); $context = stream_context_create( $contextOptions ); $download = file_get_contents( $url, false, $context ); if( empty( $download ) ) { return null; } // check for mimetype $dlFile = tempnam( $this->_shotDir, 'dl' ); file_put_contents( $dlFile, $download ); $mime = explode( '/', mime_content_type( $dlFile ) ); unlink( $dlFile ); if( $mime[0] != 'text' ) { return null; } // extract title and other information from URL $data = array( 'url' => $url ); if( preg_match( '#(.*)#iU', $download, $match ) ) { $data['title'] = trim( $match[1] ); } if( preg_match_all( '#<(h1|h2|h3|h4)[^>]*>(.*)#i', $download, $matches, PREG_SET_ORDER ) ) { $blurb = array(); foreach( $matches as $match ) { $blurb[] = strip_tags( $match[2] );; } $data['blurb'] = implode( "\n", $blurb ); } $download = ''; $id = $this->_table->save( 'screen', '__new', $data ); $q = array( $this->_table->getIdentifier( 'screen' ) => $id ); $this->_table->save( 'queue', '__new', $q ); return $id; } public function id2File( $id, $mkDir = false ) { $longId = sprintf( '%024d', $id ); $des = str_split( $longId, 3 ); array_unshift( $des, $this->_shotDir ); $name = array_pop( $des ); $des = implode( '/', $des ); if( $mkDir ) { if( !is_dir( $des ) ) { mkdir( $des, 0777, true ); } } return $des . '/' . $longId; } public function addScreenShot( $id, $file ) { $old = $this->get( $id ); if( empty( $old ) ) { return false; } $name = $this->id2File( $id, true ); WBLog::debug( 'add Screenshot ' . $name ); rename( $file, $name ); return true; } public function get( $id ) { $data = $this->_table->get( 'screen', $id ); if( empty( $data ) ) { return array(); } $file = $this->id2File( $id ); if( file_exists( $file ) ) { $data['screenshot'] = $file; } return $data; } public function delete( $id ) { $file = $this->id2File( $id ); if( file_exists( $file ) ) { unlink( $file ); } $this->_table->delete( 'queue', $id ); return $this->_table->delete( 'screen', $id ); } public function getIds( $limit = null ) { $options = array(); if( $limit ) { $options['limit'] = $limit; } $ids = $this->_table->getIds( 'screen', array(), $options ); if( empty( $ids ) ) { return array(); } return $ids; $list = array(); foreach( $ids as $id ) { $file = $this->id2File( $id ); if( file_exists( $file ) ) { $list[] = $id; } } return $list; } public function getQueue() { return $this->_table->getIds( 'queue' ); } public function unQueue( $id ) { return $this->_table->delete( 'queue', $id ); } } ?>