1:   2:   3:   4:   5:   6:   7:   8:   9:  10:  11:  12:  13:  14:  15:  16:  17:  18:  19:  20:  21:  22:  23:  24:  25:  26:  27:  28:  29:  30:  31:  32:  33:  34:  35:  36:  37:  38:  39:  40:  41:  42:  43:  44:  45:  46:  47:  48:  49:  50:  51:  52:  53:  54:  55:  56:  57:  58:  59:  60:  61:  62:  63:  64:  65:  66:  67:  68:  69:  70:  71:  72:  73:  74:  75:  76:  77:  78:  79:  80:  81:  82:  83:  84:  85:  86:  87:  88:  89:  90:  91:  92:  93:  94:  95:  96:  97:  98:  99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 
<?php
    /**
     * Copyright (C) Apis Networks, Inc - All Rights Reserved.
     *
     * Unauthorized copying of this file, via any medium, is
     * strictly prohibited without consent. Any dissemination of
     * material herein is prohibited.
     *
     * For licensing inquiries email <licensing@apisnetworks.com>
     *
     * Written by Matt Saladna <matt@apisnetworks.com>, May 2017
     */

    namespace Module\Support;

    use Crm_Module;
    use Mail;
    use Mail_Mime;
    use Module_Skeleton;
    use Util_Geoip;

    /**
     * Support structure for auth module
     */
    abstract class Auth extends Module_Skeleton
    {

        /**
         * Remap siteinfo to auth on 7.5+ platforms
         *
         * @param null|string $platform_version optional platform version to compare against
         * @return string
         */
        public static function getAuthService($platform_version = PLATFORM_VERSION): string
        {
            return version_compare($platform_version, '7.5', '>=') ?
                'auth' : 'siteinfo';
        }

        /**
         * Send security notice
         *
         * @param       $what
         * @param array $args
         * @return bool|mixed|void
         */
        protected function sendNotice($what, $args)
        {
            if (\Auth::client_ip() === '127.0.0.1') {
                // originated locally from server
                return true;
            }
            $key = 'notify.' . $what . 'change';
            $donotify = \Preferences::get($key, true);
            if (!$donotify) {
                return true;
            }

            $hostname = "UNKNOWN";
            $location = null;
            if ($tmp = $this->dns_gethostbyaddr_t($args['ip'], 3000)) {
                $hostname = $tmp;
            }
            $location = Util_Geoip::locate($args['ip']);
            $gmap = "https://www.google.com/maps/preview/@" . $location['latitude'] . "," . $location['longitude'] . ",8z";

            $vars = array_replace_recursive([
                'what'      => $what,
                'domain'    => $this->domain,
                'latitude'  => $location['latitude'],
                'longitude' => $location['longitude'],
                'city'      => $location['city'],
                'state'     => $location['state'],
                'country'   => $location['country'],
                'gmapurl'   => $gmap,
                'ip'        => $args['ip'],
                'hostname'  => $hostname,
                'username'  => $this->username,
                'config'    => [
                    'geoip' => true
                ],
            ], $args);
            if (!$vars['email']) {
                return warn("no email configured - ignoring alert notification for %s change for `%s'", $what,
                    $vars['username']);
            }
            $blade = \BladeLite::factory('views/email');
            $body = $blade->make('auth.change', $vars)->render();

            $opts = array(
                'html_charset' => 'utf-8',
                'text_charset' => 'utf-8'
            );
            $from = Crm_Module::FROM_NAME . ' <' . Crm_Module::FROM_ADDRESS . '>';
            $headers = array(
                'Sender' => $from,
                'From'   => $from
            );
            $mime = new Mail_Mime($opts);

            $mime->setHTMLBody($body);
            $mime->setTXTBody(strip_tags($body));
            $headers = $mime->txtHeaders($headers);
            $msg = $mime->get();

            return Mail::send(
                $vars['email'],
                ucwords($what) . ' Changed - ' . $vars['domain'],
                $msg,
                $headers
            );
        }

        /**
         * Recreate TokyoCabinet map
         */
        public static function rebuildMap()
        {
            if (file_exists(\Opcenter\Map::DOMAIN_MAP) && platform_is('7.5')) {
                return true;
            }

            // backwards compatible for older platforms that don't explicitly use TC
            $tcd = \Opcenter\Map::load(\Opcenter\Map::DOMAIN_MAP, 'cd');

            return $tcd->copy(\Opcenter\Map::DOMAIN_TXT_MAP);
        }
    }