0.9.0 - the ZapV version updated to current php standards. Brave search used, while Bing and Google search api are deprecated
This commit is contained in:
parent
3d4298fe8b
commit
4ddaddda3d
31 changed files with 1778 additions and 1 deletions
145
lib/ImgSearch.class.php
Executable file
145
lib/ImgSearch.class.php
Executable file
|
|
@ -0,0 +1,145 @@
|
|||
<?php
|
||||
/**
|
||||
* ZAP image search class
|
||||
*/
|
||||
class ImgSearch {
|
||||
private $hitPosition;
|
||||
|
||||
private $numberResults = 16;
|
||||
|
||||
private $folder;
|
||||
|
||||
private $word;
|
||||
|
||||
private $engine;
|
||||
|
||||
private $allowedFileTypes = array('image/jpeg','image/gif','image/png');
|
||||
|
||||
private $log;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
* @param string $imgFolder path to save images to
|
||||
* @param string $word keyword to search for
|
||||
*/
|
||||
public function __construct($imgFolder, $word) {
|
||||
$this->folder = $imgFolder;
|
||||
$this->word = $word;
|
||||
|
||||
$this->hitPosition = (int) rand(0,10);
|
||||
|
||||
if (ZAP_LOG == true) {
|
||||
$this->log = new Log($this->folder);
|
||||
}
|
||||
|
||||
$config = Config::getInstance();
|
||||
$engines = $config->retrieve('engines');
|
||||
$this->engine = $engines[array_rand($engines)];
|
||||
}
|
||||
|
||||
/**
|
||||
* download image that corresponds to $keyWord
|
||||
* returns filename of image if downloaded succeeded, or false if failed
|
||||
* @return string $localUrl / boolean
|
||||
*/
|
||||
public function getImage() {
|
||||
$imgData = $this->getImageData();
|
||||
if ($imgData == false) {
|
||||
$this->addLog('error','Search: ', 'bad search data received' );
|
||||
return false;
|
||||
}
|
||||
$this->addLog('ok', 'Engine: ', $imgData['engine']);
|
||||
|
||||
$localUrl = $this->imageToTmp($imgData['results'], $this->folder);
|
||||
|
||||
return $localUrl;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an array that contains searchdata for $word
|
||||
* @return array $searchData or false
|
||||
*/
|
||||
public function getImageData() {
|
||||
$clazz = ucfirst($this->engine);
|
||||
$search = new $clazz();
|
||||
$search->setQuery(urlencode($this->word));
|
||||
$search->setParam('start', $this->hitPosition);
|
||||
$search->setParam('size', $this->numberResults);
|
||||
|
||||
$searchData = $search->getData();
|
||||
|
||||
return $searchData['result'] == false ? false : $searchData;
|
||||
}
|
||||
|
||||
/**
|
||||
* Saves image or returns false
|
||||
* @param array $results
|
||||
* @param string $tmpDir
|
||||
* @param integer $hit
|
||||
* @return mixed
|
||||
*/
|
||||
private function imageToTmp($results, $tmpDir, $hit = 0) {
|
||||
$first = $results[$hit]['clickurl'];
|
||||
$imageName = $tmpDir . DIRECTORY_SEPARATOR . $this->filename($first);
|
||||
|
||||
$myCurl = new ZAPCurl($first, true);
|
||||
$imageData = $myCurl->fetchCurlData();
|
||||
|
||||
if($imageData == false) {
|
||||
$this->addLog('error', 'Search: ', 'received empty file' . PHP_EOL);
|
||||
return false;
|
||||
}
|
||||
|
||||
if(file_put_contents($imageName, $imageData)) {
|
||||
if($this->imageTypeOk($imageName)) {
|
||||
$this->addLog('ok', 'Search: ', "downloaded to $imageName");
|
||||
$this->addLog('ok', 'Search: ', 'source: ' . $results[$hit]['referer']);
|
||||
$this->addLog('ok', 'Search: ', 'thumb: ' . $results[$hit]['thumb'] . PHP_EOL);
|
||||
return $imageName;
|
||||
} else {
|
||||
$this->addLog('error', 'Search: ', 'wrong file type: image removed');
|
||||
unlink($imageName);
|
||||
|
||||
$nextHit = $hit + 1;
|
||||
return $this->imageToTmp($results, $tmpDir, $nextHit);
|
||||
}
|
||||
} else {
|
||||
$this->addLog('error', 'Search: ', 'failed saving image');
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Check mime type of image
|
||||
* @param string $fileUri
|
||||
* @return boolean
|
||||
*/
|
||||
private function imageTypeOk($fileUri) {
|
||||
$mimeType = mime_content_type($fileUri);
|
||||
return in_array($mimeType, $this->allowedFileTypes);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets filename from a url
|
||||
* @param string $urlString
|
||||
* @return string
|
||||
*/
|
||||
private function filename($urlString) {
|
||||
$ar = explode('/', $urlString);
|
||||
$filename = array_pop($ar);
|
||||
// Strip query string so the saved file has a real extension
|
||||
$filename = explode('?', $filename)[0];
|
||||
return $filename;
|
||||
}
|
||||
|
||||
/**
|
||||
* Log messages
|
||||
*/
|
||||
private function addLog($type, $head, $message) {
|
||||
if (ZAP_LOG == true) {
|
||||
$this->log->addToLog($type, $head, $message);
|
||||
}
|
||||
return;
|
||||
}
|
||||
}
|
||||
?>
|
||||
Loading…
Add table
Add a link
Reference in a new issue