diff options
Diffstat (limited to 'modules-available/sysconfig/addmodule_branding.inc.php')
-rw-r--r-- | modules-available/sysconfig/addmodule_branding.inc.php | 83 |
1 files changed, 48 insertions, 35 deletions
diff --git a/modules-available/sysconfig/addmodule_branding.inc.php b/modules-available/sysconfig/addmodule_branding.inc.php index d941a7a7..54b2ad57 100644 --- a/modules-available/sysconfig/addmodule_branding.inc.php +++ b/modules-available/sysconfig/addmodule_branding.inc.php @@ -11,7 +11,7 @@ class Branding_Start extends AddModule_Base { Render::addDialog(Dictionary::translateFile('config-module', 'branding_title'), false, 'branding-start', array( 'step' => 'Branding_ProcessFile', - 'edit' => $this->edit ? $this->edit->id() : false + 'edit' => $this->edit == null ? null : $this->edit->id(), )); } @@ -22,7 +22,6 @@ class Branding_ProcessFile extends AddModule_Base private $task; private $svgFile; - private $tarFile; protected function preprocessInternal() { @@ -48,14 +47,16 @@ class Branding_ProcessFile extends AddModule_Base if (strpos($url, '://') === false) $url = "http://$url"; $title = false; - if (!$this->downloadSvg($this->svgFile, $url, $title)) + if (!Branding_ProcessFile::downloadSvg($this->svgFile, $url, $title)) { + @unlink($this->svgFile); Util::redirect('?do=SysConfig&action=addmodule&step=Branding_Start'); + } Session::set('logo_name', $title); } chmod($this->svgFile, 0644); - $this->tarFile = '/tmp/bwlp-' . time() . '-' . mt_rand() . '.tgz'; + $tarFile = '/tmp/bwlp-' . time() . '-' . mt_rand() . '.tgz'; $this->task = Taskmanager::submit('BrandingGenerator', array( - 'tarFile' => $this->tarFile, + 'tarFile' => $tarFile, 'svgFile' => $this->svgFile )); $this->task = Taskmanager::waitComplete($this->task, 5000); @@ -64,8 +65,7 @@ class Branding_ProcessFile extends AddModule_Base Taskmanager::addErrorMessage($this->task); Util::redirect('?do=SysConfig&action=addmodule&step=Branding_Start'); } - Session::set('logo_tgz', $this->tarFile); - Session::save(); + Session::set('logo_tgz', $tarFile); } protected function renderInternal() @@ -75,13 +75,13 @@ class Branding_ProcessFile extends AddModule_Base $png = base64_encode(file_get_contents($this->task['data']['pngFile'])); if (filesize($this->svgFile) < 1000000) $svg = base64_encode(file_get_contents($this->svgFile)); - Render::addDialog(Dictionary::translate('config-module', 'branding_title'), false, 'branding-check', array( + Render::addDialog(Dictionary::translateFile('config-module', 'branding_title'), false, 'branding-check', array( 'png' => $png, 'svg' => $svg, - 'error' => $this->task['data']['error'], + 'error' => $this->task['data']['error'] ?? $this->task['statusCode'], 'step' => 'Branding_Finish', - 'edit' => $this->edit ? $this->edit->id() : false, - 'title' => $this->edit ? $this->edit->title() : false + 'edit' => $this->edit === null ? null : $this->edit->id(), + 'title' => $this->edit === null ? null : $this->edit->title(), ) ); @unlink($this->svgFile); @@ -96,39 +96,56 @@ class Branding_ProcessFile extends AddModule_Base * @return boolean true of download succeeded, false on download error (also returns true if downloaded file doesn't * seem to be svg!) */ - private function downloadSvg($svgName, $url, &$title) + private static function downloadSvg(string $svgName, string $url, &$title): bool { $title = false; - // [wikipedia] Did someone paste a link to a thumbnail of the svg? Let's fix that... - if (preg_match('#^(.*)/thumb/(.*\.svg)/.*\.svg#', $url, $out)) { - $url = $out[1] . '/' . $out[2]; - } for ($i = 0; $i < 5; ++$i) { + // [wikipedia] Did someone paste a link to a thumbnail of the svg? Let's fix that... + if (preg_match('#^(.*)/thumb/(.*\.svg)/.*\.svg#', $url, $out)) { + $url = $out[1] . '/' . $out[2]; + } $code = 400; if (!Download::toFile($svgName, $url, 3, $code) || $code < 200 || $code > 299) { Message::addError('remote-timeout', $url, $code); return false; } - $content = FileUtil::readFile($svgName, 25000); + $content = FileUtil::readFile($svgName, 250000); // Is svg file? if (strpos($content, '<svg') !== false) return true; // Found an svg tag - don't try to find links to the actual image // [wikipedia] Try to be nice and detect links that might give a hint where the svg can be found - if (preg_match_all('#href="([^"]*upload.wikimedia.org/[^"]*/[^"]*/[^"]*\.svg|[^"]+/[^"]+:[^"]+\.svg[^"]*)"#', $content, $out, PREG_PATTERN_ORDER)) { + $out1 = $out2 = $out3 = null; + if (preg_match_all('#href="([^"]*upload.wikimedia.org/[^"]*/[^"]*/[^"]*\.svg)"#', $content, $out1, PREG_PATTERN_ORDER) + || preg_match_all('#src="([^"]*upload.wikimedia.org/[^"]*/thumb/[^"]*\.svg/[^"]+\.svg[^"]*)"#', $content, $out2, PREG_PATTERN_ORDER) + || preg_match_all('#href="([^"]+/[^"]+:[^"]+\.svg)"#', $content, $out3, PREG_PATTERN_ORDER)) { if ($title === false && preg_match('#<title>([^<]*)</title>#i', $content, $tout)) { $title = trim(preg_replace('/\W*Wikipedia.*/', '', $tout[1])); } $new = false; - foreach ($out[1] as $res) { + $out = []; + if (isset($out1[1])) { + $out += $out1[1]; + } + if (isset($out2[1])) { + $out += $out2[1]; + } + if (isset($out3[1])) { + $out += $out3[1]; + } + foreach ($out as $res) { + error_log("Match '$res'"); + if (!preg_match('/hochschule|univers|logo|siegel/i', $res)) + continue; if (strpos($res, 'action=edit') !== false) continue; - $new = $this->internetCombineUrl($url, html_entity_decode($res, ENT_COMPAT, 'UTF-8')); + $new = Branding_ProcessFile::internetCombineUrl($url, html_entity_decode($res, ENT_COMPAT, 'UTF-8')); if ($new !== $url) break; } if ($new === $url || $new === false) break; + error_log("New: '$new'"); $url = $new; continue; } @@ -145,7 +162,7 @@ class Branding_ProcessFile extends AddModule_Base * @param string $relative relative url that will be converted to an absolute url * @return string combined absolute url */ - private function internetCombineUrl($absolute, $relative) + private static function internetCombineUrl(string $absolute, string $relative): string { $p = parse_url($relative); if (!empty($p["scheme"])) @@ -154,8 +171,8 @@ class Branding_ProcessFile extends AddModule_Base $parsed = parse_url($absolute); $path = dirname($parsed['path']); - if ($relative{0} === '/') { - if ($relative{1} === '/') + if ($relative[0] === '/') { + if ($relative[1] === '/') return "{$parsed['scheme']}:$relative"; $cparts = array_filter(explode("/", $relative)); } else { @@ -197,9 +214,9 @@ class Branding_Finish extends AddModule_Base protected function preprocessInternal() { $title = Request::post('title'); - if ($title === false || empty($title)) + if (empty($title)) $title = Session::get('logo_name'); - if ($title === false || empty($title)) { + if (empty($title)) { Message::addError('missing-title'); // TODO: Ask for title again instead of starting over Util::redirect('?do=SysConfig&action=addmodule&step=Branding_Start'); } @@ -208,30 +225,26 @@ class Branding_Finish extends AddModule_Base Message::addError('main.error-read', $tgz); Util::redirect('?do=SysConfig&action=addmodule&step=Branding_Start'); } - if ($this->edit === false) + if ($this->edit === null) { $module = ConfigModule::getInstance('Branding'); - else + } else { $module = $this->edit; - if ($module === false) { - Message::addError('main.error-read', 'branding.inc.php'); - Util::redirect('?do=SysConfig&action=addmodule&step=Branding_Start'); } $module->setData('tmpFile', $tgz); - if ($this->edit !== false) + if ($this->edit !== null) $ret = $module->update($title); else $ret = $module->insert($title); if (!$ret) Util::redirect('?do=SysConfig&action=addmodule&step=Branding_Start'); - elseif ($module->generate($this->edit === false, NULL, 200) === false) + elseif ($module->generate($this->edit === null, NULL, 200) === false) Util::redirect('?do=SysConfig&action=addmodule&step=Branding_Start'); Session::set('logo_tgz', false); Session::set('logo_name', false); - Session::save(); // Yay - if ($this->edit !== false) + if ($this->edit !== null) { Message::addSuccess('module-edited'); - else { + } else { Message::addSuccess('module-added'); AddModule_Base::setStep('AddModule_Assign', $module->id()); return; |