Skip to content

Commit

Permalink
1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
lianglee committed Oct 13, 2024
0 parents commit e074a19
Show file tree
Hide file tree
Showing 13 changed files with 785 additions and 0 deletions.
56 changes: 56 additions & 0 deletions actions/generate.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php
/**
* Open Source Social Network
*
* @package Open Source Social Network
* @author OSSN Core Team <[email protected]>
* @copyright (C) Engr. Arsalan Shah
* @license Open Source Social Network License (OSSN LICENSE) http://www.opensource-socialnetwork.org/licence
* @link https://www.opensource-socialnetwork.org/
*/
$sw = input('service_worker');

if($sw == 'enabled') {
//copied from original sw
$siteurl = ossn_site_url();
if(substr($siteurl, 0, 8) != 'https://') {
ossn_trigger_message(ossn_print('favicon:generator:sw:https:error'), 'error');
redirect(REF);
}
}
$logo = new OssnFile();
$logo->setFile('logo');
$logo->setExtension(array(
'jpg',
'png',
'jpeg',
'jfif',
'gif',
'webp',
));
if(isset($logo->file['tmp_name']) && $logo->typeAllowed()) {
$file = $logo->file['tmp_name'];

$info = getimagesize($file);
$width = $info[0];
$height = $info[1];

if($width != $height) {
ossn_trigger_message(ossn_print('favicon:generator:logo:dim:error'), 'error');
redirect(REF);
}

$time = time();

$icon = new Favicon\Generator($file, $time);
if($sw == 'enabled') {
$icon->serviceworker = true;
}
$icon->generate();

ossn_trigger_message(ossn_print('favicon:generator:logo:done'));
redirect(REF);
} else {
ossn_trigger_message(ossn_print('favicon:generator:logo:error'), 'error');
redirect(REF);
}
13 changes: 13 additions & 0 deletions actions/reset.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php
/**
* Open Source Social Network
*
* @package Open Source Social Network
* @author OSSN Core Team <[email protected]>
* @copyright (C) Engr. Arsalan Shah
* @license Open Source Social Network License (OSSN LICENSE) http://www.opensource-socialnetwork.org/licence
* @link https://www.opensource-socialnetwork.org/
*/
$path = ossn_route()->www . 'sfavicons/';
\OssnFile::DeleteDir($path);
redirect(REF);
188 changes: 188 additions & 0 deletions classes/Generator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,188 @@
<?php
/**
* Open Source Social Network
*
* @package Open Source Social Network
* @author OSSN Core Team <[email protected]>
* @copyright (C) Engr. Arsalan Shah
* @license Open Source Social Network License (OSSN LICENSE) http://www.opensource-socialnetwork.org/licence
* @link https://www.opensource-socialnetwork.org/
*/
namespace Favicon;
class Generator {
public function __construct($file, $time) {
$this->file = $file;
$this->time = $time;
$this->serviceworker = false;
}
public function browserXML() {
$file = __FaviconGenerator__ . 'configs/browserconfig.xml.dist';
$file = file_get_contents($file);

$urllogo_310 = ossn_site_url("sfavicons/favicon-310x310.png?v{$this->time}");
$urllogo_512 = ossn_site_url("sfavicons/favicon-512x512.png?v{$this->time}");

$newfile = str_replace('{512logo_url}', $urllogo_512, $file);
$newfile = str_replace('{310logo_url}', $urllogo_310, $newfile);
return $newfile;
}
public function webManifest() {
$urllogo_192 = ossn_site_url("sfavicons/favicon-192x192.png?v{$this->time}");
$urllogo_512 = ossn_site_url("sfavicons/favicon-512x512.png?v{$this->time}");

$sitename = ossn_site_settings('site_name');

$manifest = array(
'name' => $sitename,
'short_name' => $sitename,
'icons' => array(
array(
'src' => $urllogo_192,
'sizes' => '192x192',
'type' => 'image/png',
'purpose' => 'any maskable',
),
array(
'src' => $urllogo_512,
'sizes' => '512x512',
'type' => 'image/png',
'purpose' => 'any maskable',
),
),

'theme_color' => '#ffffff',
'background_color' => '#ffffff',
'start_url' => ossn_site_url(),
'display' => 'standalone',
'orientation' => 'any',
'scope' => ossn_site_url(),
);
if(isset($this->serviceworker) && $this->serviceworker === true) {
$manifest['serviceworker'] = array(
'src' => ossn_site_url('sfavicons/sw.js'),
);
}
return json_encode($manifest, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
}
public function html() {
return ossn_plugin_view('favicon_generator/html', array(
'time' => $this->time,
));
}
public function yandex() {
$manifest = array(
'version' => '1.0',
'api_version' => 1,
'layout' => array(
'logo' => ossn_site_url("sfavicons/favicon-50x50.png?v{$this->time}"),
'color' => 'background_color',
'show_title' => true,
),
);
return json_encode($manifest, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
}
public function ico($png32, $destination) {
if(!is_file($png32) || !is_dir($destination)) {
return false;
}
require_once __FaviconGenerator__ . 'vendors/png2ico/php2ico.php';
$icon = new \php2ico($png32);
return $icon->save_ico($destination . 'favicon.ico');
}
public function resize($input_name, $maxwidth, $maxheight) {
// Get the size information from the image
$imgsizearray = getimagesize($input_name);
if($imgsizearray == false) {
return false;
}
$image = new \OssnImage($input_name);

$accepted_formats = array(
'image/jpeg' => 'jpeg',
'image/pjpeg' => 'jpeg',
'image/png' => 'png',
'image/x-png' => 'png',
'image/webp' => 'webp',
);

// make sure the function is available
$load_function = 'imagecreatefrom' . $accepted_formats[$imgsizearray['mime']];
if(!is_callable($load_function)) {
return false;
}
$image->resizeToBestFit($maxwidth, $maxheight);
return $image->getImageAsString(IMAGETYPE_PNG, 50);
}
public function generate() {
if(!is_file($this->file)) {
return false;
}
$path = ossn_route()->www . 'sfavicons/';

//delete old dir and create again
\OssnFile::DeleteDir($path);
mkdir($path, 0755, true);

//png icon files
$config = array(
16,
32,
37,
48,
50,
57,
60,
72,
76,
96,
114,
120,
144,
152,
180,
192,
150,
310,
512,
);
foreach ($config as $size) {
$new = $this->resize($this->file, $size, $size, false);
file_put_contents($path . "favicon-{$size}x{$size}.png", $new);
}

//ico file
//we need to use 32px PNG file
if(!$this->ico($path . 'favicon-32x32.png', $path)) {
//failed for some reason
//delete generated icons
error_log("FaviconGenerator: Failed to generate ICO file from {$path}");
\OssnFile::DeleteDir($path);
return false;
}

//webmanifest
$manifest = $this->webManifest();
file_put_contents($path . 'manifest.json', $manifest);

//yandex
$yandex = $this->yandex();
file_put_contents($path . 'yandex-browser-manifest.json', $yandex);

//browserxml
$browserxml = $this->browserXML();
file_put_contents($path . 'browserconfig.xml', $browserxml);

//header
$html = $this->html();
file_put_contents($path . 'head.html', $html);

//service worker
if(isset($this->serviceworker) && $this->serviceworker === true) {
$sw = ossn_plugin_view('favicon_generator/favicon-sw');
file_put_contents($path . 'sw.js', $sw);
}

//generated settings
file_put_contents($path . 'generated', 1);
}
}
10 changes: 10 additions & 0 deletions configs/browserconfig.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<browserconfig>
<msapplication>
<tile>
<square150x150logo src="{512logo_url}"/>
<square310x310logo src="{310logo_url}"/>
<TileColor>#2b5797</TileColor>
</tile>
</msapplication>
</browserconfig>
25 changes: 25 additions & 0 deletions locale/ossn.en.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php
/**
* Open Source Social Network
*
* @package Open Source Social Network
* @author OSSN Core Team <[email protected]>
* @copyright (C) Engr. Arsalan Shah
* @license Open Source Social Network License (OSSN LICENSE) http://www.opensource-socialnetwork.org/licence
* @link https://www.opensource-socialnetwork.org/
*/
ossn_register_languages('en', array(
'favicons:generator:select' => 'Select',
'favicons:generator:enabled' => 'Enabled',
'favicons:generator:disabled' => 'Disabled',
'favicon:generator:sw:https:error' => 'Enabling PWA / service Worker not possible - Your site must be HTTPS supported.',
'favicon:generator:logo:dim:error' => 'Logo must be square and at least 512px * 512px',
'favicon:generator:logo:info' => 'Logo File (at least 512px * 512px and square)',
'favicon:generator:logo:info:2' => 'Make sure logo file is no more than 1MB',
'favicon:generator:sw' => 'Enable Service Worker',
'favicon:generator:sw:info' => 'When Enabled it will enable progressive web app (PWA). Allowing users to install PWA on supported device. Make sure you are using SSL protocol (https).',
'favicon:generator:logo:error' => 'Invalid Logo file',
'favicon:generator:logo:done' => 'Logo has been generated',
'favicon:generator:reset' => 'Reset Now',
'favicon:generator:reset:info' => 'Icons are already generated. You can reset to change the favicon for you website.',
));
52 changes: 52 additions & 0 deletions ossn_com.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php
/**
* Open Source Social Network
*
* @package Open Source Social Network
* @author OSSN Core Team <[email protected]>
* @copyright (C) Engr. Arsalan Shah
* @license Open Source Social Network License (OSSN LICENSE) http://www.opensource-socialnetwork.org/licence
* @link https://www.opensource-socialnetwork.org/
*/

define('__FaviconGenerator__', ossn_route()->com . 'FaviconGenerator/');
ossn_register_class(array(
'Favicon\Generator' => __FaviconGenerator__ . 'classes/Generator.php',
));
ossn_register_callback('ossn', 'init', function () {
if(ossn_isAdminLoggedin()) {
ossn_register_com_panel('FaviconGenerator', 'settings');
ossn_register_action('favicon_generator/settings', __FaviconGenerator__ . 'actions/generate.php');
ossn_register_action('favicon_generator/reset', __FaviconGenerator__ . 'actions/reset.php');
}
//taken from original favicons component
ossn_add_hook('page', 'override:view', '_com_favicons_page_handler');
ossn_extend_view('ossn/admin/head', '_com_favicons_add_head_tags');

//from original service worker
ossn_extend_view('ossn/site/head', '_com_serviceworker_enable_service_worker');
ossn_extend_view('ossn/admin/head', '_com_serviceworker_enable_service_worker');
});
function _com_serviceworker_enable_service_worker() {
$sw_file = ossn_route()->www . 'sfavicons/sw.js';
$sw_url = ossn_site_url() . 'sfavicons/sw.js';
if(file_exists($sw_file)) {
$script =
"\n<script>if('serviceWorker' in navigator) { navigator.serviceWorker.register(\"" . $sw_url . "\").then(function() { /* console.log('Service Worker Registered'); */ });}</script>\n";
return $script;
}
}
function _com_favicons_page_handler($hook, $type, $return, $params) {
if($params['handler'] != 'shared_content') {
ossn_extend_view('ossn/site/head', '_com_favicons_add_head_tags');
}
}

function _com_favicons_add_head_tags() {
$tags_file = ossn_route()->www . 'sfavicons/head.html';
if(file_exists($tags_file)) {
$content = file_get_contents($tags_file, true);
$tags = "\n" . $content . "\n";
return $tags;
}
}
20 changes: 20 additions & 0 deletions ossn_com.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="UTF-8"?>
<component xmlns="http://www.opensource-socialnetwork.org/v/3.0">
<name>Favicon Generator</name>
<id>FaviconGenerator</id>
<author>Core Team</author>
<description>Favicon Generator</description>
<license>Open Source Social Network License (OSSN LICENSE) v4.0</license>
<author_url>https://www.opensource-socialnetwork.org.com/</author_url>
<license_url>http://www.opensource-socialnetwork.org/licence</license_url>
<version>1.0</version>
<requires>
<type>ossn_version</type>
<version>7.6</version>
</requires>
<requires>
<type>ossn_component</type>
<name>Favicons</name>
<comparator>disabled</comparator>
</requires>
</component>
13 changes: 13 additions & 0 deletions plugins/default/favicon_generator/favicon-sw-unregister.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
self.addEventListener('install', function(e) {
self.skipWaiting();
});

self.addEventListener('activate', function(e) {
self.registration.unregister()
.then(function() {
return self.clients.matchAll();
})
.then(function(clients) {
clients.forEach(client => client.navigate(client.url))
});
});
Loading

0 comments on commit e074a19

Please sign in to comment.