file = WBClass::create('WBFile'); $this->config = WBClass::create('WBConfig'); $this->config->load('ute/qrcode/config', true); $this->setupCache(); } /** * Setup Cache Dir * */ private function setupCache() { if (!WBParam::get('wb/cache/use', true)) { return; } $this->cacheDir = '/var/cache/qrcode'; $this->file->mkdir($this->cacheDir); $this->file->clear(); } /** * Set Config Parameter * * Allows to override parameter from config file dynamically * * @param array list of parameter */ public function configure($config = array()) { $this->configParam = array_merge($this->configParam, $config); } /** * Set QRCode Text * * @param string text for QR code */ public function setText($text) { $text = trim($text); if ($text == $this->text) { return; } $this->file->unlink(); $this->text = $text; } /** * Delete QRCode Image * */ public function flush() { if (empty($this->cacheDir)) { $this->file->unlink(); return; } $this->file->clear(); } /** * Render QRCode Image and Return File * * @return WBFile */ public function getImage() { if ($this->file->isFile()) { return $this->file; } $renderer = strtolower($this->config->get('qrcode/renderer', 'command')); $config = $this->config->get('qrcode/config/' . $renderer, array()); $config = array_merge($config, $this->configParam); if (empty($this->cacheDir)) { $this->file->tempnam('qrcode'); } else { $cid = md5(serialize($config) . '-text-' . $this->text); if ($this->file->exists($this->cacheDir . '/' . $cid)) { return $this->file; } $this->file->touch($this->cacheDir . '/' . $cid); } switch ($renderer) { case 'webservice': $this->renderFromWebService($config); break; default: case 'command': $this->renderFromCommand($config); break; } return $this->file; } /** * Use Command * * Use Command-Line-Tool to render QR-Code. * Requires Debian Package go-qrcode * @param array */ private function renderFromCommand($config) { $config = array_merge(array( 'size' => 800, 'blackonwhite' => 1, 'command' => 'qrcode', 'border' => 1, 'hasfeatureborder' => 0 ), $config); $config['blackonwhite'] = intval($config['blackonwhite']); $config['size'] = intval($config['size']); $config['size'] = max(100, min(2000, $config['size'])); $cmd = array($config['command']); if (1 > $config['blackonwhite']) { $cmd[] = '-i'; } // disable border if (0 < $config['hasfeatureborder'] && 1 > intval($config['border'])) { $cmd[] = '-d'; } $cmd[] = '-s ' . $config['size']; $cmd[] = '%s >%s'; $cmd = implode(' ', $cmd); $cmd = sprintf($cmd, escapeshellarg($this->text), $this->file->realpath()); exec($cmd, $out, $ret); if (0 < $ret) { $this->debugPrint('FAILED to generate QR-Code with command: ' . $cmd, -1); $this->debugPrint('Output of command ' . implode("\n", $out), 5); } else { $this->debugPrint('Generate QR-Code with command: ' . $cmd, 5); } } /** * Use API * * Use Web-service to render QR-Code. * @param array */ private function renderFromWebService($config) { $config = array_merge(array( 'size' => 800, 'url' => 'https://api.qrserver.com/v1/create-qr-code/' ), $config); $config['size'] = intval($config['size']); $config['size'] = max(100, min(2000, $config['size'])); $text = urlencode($this->text); $png = file_get_contents(sprintf('%s?size=%sx%s&data=%s', $config['url'], $config['size'], $config['size'] ,$text)); file_put_contents($this->file->realpath(), $png); } }