Fix coding standards issues

This commit is contained in:
fabrixxm 2017-04-14 09:13:19 +02:00
parent c91b574f3a
commit 41da664692

View file

@ -6,88 +6,104 @@
* Author: Fabio Comuni <http://kirgroup.com/profile/fabrixxm> * Author: Fabio Comuni <http://kirgroup.com/profile/fabrixxm>
*/ */
require_once "include/Emailer.php"; require_once 'include/Emailer.php';
/* because the fraking openpgp-php is in composer, require libs in composer /* because the fraking openpgp-php is in composer, require libs in composer
* and then don't use autoloader to load classes... */ * and then don't use autoloader to load classes... */
$path = __DIR__."/vendor/phpseclib/phpseclib/phpseclib/"; $path = __DIR__ . '/vendor/phpseclib/phpseclib/phpseclib/';
set_include_path(get_include_path() . PATH_SEPARATOR . $path); set_include_path(get_include_path() . PATH_SEPARATOR . $path);
/* so, we don't use the autoloader and include what we need */ /* so, we don't use the autoloader and include what we need */
$path = __DIR__."/vendor/singpolyma/openpgp-php/lib"; $path = __DIR__ . '/vendor/singpolyma/openpgp-php/lib';
set_include_path(get_include_path() . PATH_SEPARATOR . $path); set_include_path(get_include_path() . PATH_SEPARATOR . $path);
require_once "openpgp.php"; require_once 'openpgp.php';
require_once "openpgp_crypt_symmetric.php"; require_once 'openpgp_crypt_symmetric.php';
function securemail_install() { function securemail_install() {
register_hook('plugin_settings', 'addon/securemail/securemail.php', 'securemail_settings'); register_hook('plugin_settings', 'addon/securemail/securemail.php', 'securemail_settings');
register_hook('plugin_settings_post', 'addon/securemail/securemail.php', 'securemail_settings_post'); register_hook('plugin_settings_post', 'addon/securemail/securemail.php', 'securemail_settings_post');
register_hook('emailer_send_prepare', 'addon/securemail/securemail.php', 'securemail_emailer_send_prepare'); register_hook('emailer_send_prepare', 'addon/securemail/securemail.php', 'securemail_emailer_send_prepare');
logger("installed securemail"); logger('installed securemail');
} }
function securemail_uninstall() { function securemail_uninstall() {
unregister_hook('plugin_settings', 'addon/securemail/securemail.php', 'securemail_settings'); unregister_hook('plugin_settings', 'addon/securemail/securemail.php', 'securemail_settings');
unregister_hook('plugin_settings_post', 'addon/securemail/securemail.php', 'securemail_settings_post'); unregister_hook('plugin_settings_post', 'addon/securemail/securemail.php', 'securemail_settings_post');
unregister_hook('emailer_send_prepare', 'addon/securemail/securemail.php', 'securemail_emailer_send_prepare'); unregister_hook('emailer_send_prepare', 'addon/securemail/securemail.php', 'securemail_emailer_send_prepare');
logger("removed securemail"); logger('removed securemail');
} }
/**
function securemail_settings(&$a,&$s){ * @brief Build user settings form
if(! local_user()) { *
* @link https://github.com/friendica/friendica/blob/develop/doc/Plugins.md#plugin_settings 'plugin_settings' hook
*
* @param App $a App instance
* @param string $s output html
*
* @see App
*/
function securemail_settings(App &$a, string &$s){
if (!local_user()) {
return; return;
} }
$enable = intval(get_pconfig(local_user(),'securemail','enable')); $enable = intval(get_pconfig(local_user(), 'securemail', 'enable'));
$publickey = get_pconfig(local_user(),'securemail','pkey'); $publickey = get_pconfig(local_user(), 'securemail', 'pkey');
$t = get_markup_template( "admin.tpl", "addon/securemail/" ); $t = get_markup_template('admin.tpl', 'addon/securemail/');
$s = replace_macros($t, array( $s = replace_macros($t, array(
'$title' => t('"Secure Mail" Settings'), '$title' => t('"Secure Mail" Settings'),
'$submit' => t('Save Settings'), '$submit' => t('Save Settings'),
'$test' => t('Save and send test'), //NOTE: update also in 'post' '$test' => t('Save and send test'), //NOTE: update also in 'post'
'$enable' => array('securemail-enable', t('Enable Secure Mail'), $enable, ""), '$enable' => array('securemail-enable', t('Enable Secure Mail'), $enable, ''),
'$publickey' => array('securemail-pkey', t('Public key'), $publickey, t("Your public PGP key, ascii armored format"), "rows='10'") '$publickey' => array('securemail-pkey', t('Public key'), $publickey, t('Your public PGP key, ascii armored format'), 'rows="10"')
)); ));
return;
} }
function securemail_settings_post(&$a, &$b){
if(! local_user()) { /**
* @brief Handle data from user settings form
*
* @link https://github.com/friendica/friendica/blob/develop/doc/Plugins.md#plugin_settings_post 'plugin_settings_post' hook
*
* @param App $a App instance
* @param array $b hook data
*
* @see App
*/
function securemail_settings_post(App &$a, array &$b){
if (!local_user()) {
return; return;
} }
if($_POST['securemail-submit']) { if ($_POST['securemail-submit']) {
set_pconfig(local_user(),'securemail','pkey',trim($_POST['securemail-pkey'])); set_pconfig(local_user(), 'securemail', 'pkey', trim($_POST['securemail-pkey']));
$enable = ((x($_POST,'securemail-enable')) ? 1 : 0); $enable = ((x($_POST, 'securemail-enable')) ? 1 : 0);
set_pconfig(local_user(),'securemail','enable', $enable); set_pconfig(local_user(), 'securemail', 'enable', $enable);
info( t('Secure Mail Settings saved.') . EOL); info(t('Secure Mail Settings saved.') . EOL);
if ($_POST['securemail-submit'] == t('Save and send test')) { if ($_POST['securemail-submit'] == t('Save and send test')) {
$sitename = $a->config['sitename']; $sitename = $a->config['sitename'];
$hostname = $a->get_hostname(); $hostname = $a->get_hostname();
if (strpos($hostname, ':')){ if (strpos($hostname, ':')) {
$hostname = substr($hostname, 0, strpos($hostname, ':')); $hostname = substr($hostname, 0, strpos($hostname, ':'));
} }
$sender_email = $a->config['sender_email']; $sender_email = $a->config['sender_email'];
if (empty($sender_email)){ if (empty($sender_email)) {
$sender_email = 'noreply@'.$hostname; $sender_email = 'noreply@' . $hostname;
} }
$subject = "Friendica - Secure Mail - Test"; $subject = 'Friendica - Secure Mail - Test';
$message = "This is a test message from your Friendica Secure Mail addon.\n\nBye!"; $message = 'This is a test message from your Friendica Secure Mail addon.\n\nBye!';
$params = array( $params = array(
'uid' => local_user(), 'uid' => local_user(),
@ -100,35 +116,45 @@ function securemail_settings_post(&$a, &$b){
); );
// enable addon for test // enable addon for test
set_pconfig(local_user(),'securemail','enable', 1); set_pconfig(local_user(), 'securemail', 'enable', 1);
$res = Emailer::send($params); $res = Emailer::send($params);
// revert to saved value // revert to saved value
set_pconfig(local_user(),'securemail','enable', $enable); set_pconfig(local_user(), 'securemail', 'enable', $enable);
if ($res) { if ($res) {
info( t("Test email sent") . EOL); info(t('Test email sent') . EOL);
} else { } else {
notice( t("There was an error sending the test email") .EOL); notice(t('There was an error sending the test email') . EOL);
} }
} }
} }
} }
function securemail_emailer_send_prepare(&$a, &$b) { /**
* @brief Encrypt notification emails text
*
* @link https://github.com/friendica/friendica/blob/develop/doc/Plugins.md#emailer_send_prepare 'emailer_send_prepare' hook
*
* @param App $a App instance
* @param array $b hook data
*
* @see App
*/
function securemail_emailer_send_prepare(App &$a, array &$b) {
if (!x($b,'uid')) { if (!x($b,'uid')) {
return; return;
} }
$uid = $b['uid']; $uid = $b['uid'];
$enable_checked = get_pconfig($uid,'securemail','enable'); $enable_checked = get_pconfig($uid, 'securemail', 'enable');
if (!$enable_checked) { if (!$enable_checked) {
return; return;
} }
$public_key_ascii = get_pconfig($uid,'securemail','pkey'); $public_key_ascii = get_pconfig($uid, 'securemail', 'pkey');
preg_match('/-----BEGIN ([A-Za-z ]+)-----/', $public_key_ascii, $matches); preg_match('/-----BEGIN ([A-Za-z ]+)-----/', $public_key_ascii, $matches);
$marker = (empty($matches[1])) ? 'MESSAGE' : $matches[1]; $marker = (empty($matches[1])) ? 'MESSAGE' : $matches[1];
@ -141,7 +167,12 @@ function securemail_emailer_send_prepare(&$a, &$b) {
'filename' => 'encrypted.gpg' 'filename' => 'encrypted.gpg'
)); ));
$encrypted = OpenPGP_Crypt_Symmetric::encrypt($key, new OpenPGP_Message(array($data))); $encrypted = OpenPGP_Crypt_Symmetric::encrypt($key, new OpenPGP_Message(array($data)));
$armored_encrypted = wordwrap(OpenPGP::enarmor($encrypted->to_bytes(), "PGP MESSAGE"), 64, "\n", true); $armored_encrypted = wordwrap(
OpenPGP::enarmor($encrypted->to_bytes(), 'PGP MESSAGE'),
64,
'\n',
true
);
$b['textVersion'] = $armored_encrypted; $b['textVersion'] = $armored_encrypted;
$b['htmlVersion'] = null; $b['htmlVersion'] = null;
@ -153,7 +184,7 @@ function securemail_emailer_send_prepare(&$a, &$b) {
function securemail_autoloader() { function securemail_autoloader() {
$loader = require dirname(dirname(__DIR__))."/vendor/autoload.php"; $loader = require dirname(dirname(__DIR__)) . '/vendor/autoload.php';
$map = require __DIR__ . '/vendor/composer/autoload_namespaces.php'; $map = require __DIR__ . '/vendor/composer/autoload_namespaces.php';
foreach ($map as $namespace => $path) { foreach ($map as $namespace => $path) {