2015年12月28日 星期一

遠振主機 escapeshellarg() has been disabled for security reasons

用遠振主機又踩雷啦,遠振將escapeshellarg這個函式disabled 所以只要有用到 SebastianBergmann\Environment\Runtime Symfony\Component\Console\Input\Input Symfony\Component\HttpFoundation\File\MimeType\FileBinaryMimeTypeGuesser Symfony\Component\Process\ProcessUtils Tracy\Debugger 都會提示 escapeshellarg() has been disabled for security reasons 不過還好php5.3之後就支援namespace,可以讓我們輕鬆略過這個問題 (如果使用的套件不支援namespace那就只好認命的去修改程式囉) 只要require下面的檔案就可以解決這些問題了

namespace Yuan\Jhen
{
    if (function_exists('escapeshellarg') === true) {
        function escapeshellarg($input)
        {
            return \escapeshellarg($input);
        }
    } else {
        function escapeshellarg($input)
        {
            $input = str_replace('\'', '\\\'', $input);

            return '\''.$input.'\'';
        }
    }
}

namespace SebastianBergmann\Environment
{
    function escapeshellarg($input)
    {
        return \Yuan\Jhen\escapeshellarg($input);
    }
}

namespace Symfony\Component\Console\Input
{
    function escapeshellarg($input)
    {
        return \Yuan\Jhen\escapeshellarg($input);
    }
}

namespace Symfony\Component\HttpFoundation\File\MimeType
{
    function escapeshellarg($input)
    {
        return \Yuan\Jhen\escapeshellarg($input);
    }
}

namespace Symfony\Component\Process
{
    function escapeshellarg($input)
    {
        return \Yuan\Jhen\escapeshellarg($input);
    }
}

namespace Tracy
{
    function escapeshellarg($input)
    {
        return \Yuan\Jhen\escapeshellarg($input);
    }
}

2015年12月22日 星期二

利用Git來升級Laravel

照著影片操作,就會出現conflicts,處理完conflicts之後,再執行

composer install

這樣大致上就升級完畢了,而且如果升級出問題還可以利用git本身的功能來進行還原...


2015年12月20日 星期日

php 偵測語系

function getDefaultLanguage()
{
    if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
        return parseDefaultLanguage($_SERVER['HTTP_ACCEPT_LANGUAGE']);
    } else {
        return parseDefaultLanguage(null);
    }
}

function parseDefaultLanguage($http_accept, $deflang = 'zh-TW')
{
    if (isset($http_accept) && strlen($http_accept) > 1) {
        # Split possible languages into array
        $x = explode(',', $http_accept);
        foreach ($x as $val) {
            #check for q-value and create associative array. No q-value means 1 by rule
            if (preg_match("/(.*);q=([0-1]{0,1}.\d{0,4})/i", $val, $matches)) {
                $lang[$matches[1]] = (float) $matches[2];
            } else {
                $lang[$val] = 1.0;
            }
        }

        #return default language (highest q-value)
        $qval = 0.0;
        foreach ($lang as $key => $value) {
            if ($value > $qval) {
                $qval = (float) $value;
                $deflang = $key;
            }
        }
    }

    return strtolower($deflang);
}

echo getDefaultLanguage();

資料庫備份工具

class DatabaseClone
{
    public $link;

    public function __construct($host, $username, $password)
    {
        $this->link = mysqli_connect($host, $username, $password);
        mysqli_query($this->link, 'SET NAMES utf8');
        mysqli_query($this->link, 'SET AUTOCOMMIT = 0');
        mysqli_query($this->link, 'SET UNIQUE_CHECKS = 0');
        mysqli_query($this->link, 'SET FOREIGN_KEY_CHECKS = 0');
    }

    public function fetch($query)
    {
        $results = [];
        $query = mysqli_query($this->link, $query);
        while ($row = mysqli_fetch_assoc($query)) {
            $results[] = $row;
        }

        return $results;
    }

    public function copy($copyTo)
    {
        set_time_limit(-1);
        ini_set('memory_limit', -1);
        $query = mysqli_query($this->link, $sql = 'show databases');
        while ($database = mysqli_fetch_assoc($query)) {
            $database = $database['Database'];
            if (in_array($database, ['mysql', 'information_schema', 'performance_schema'], true) === true) {
                continue;
            }

            $createDatabase = mysqli_fetch_assoc(mysqli_query($this->link, 'SHOW CREATE DATABASE `'.$database.'`'));
            mysqli_query($copyTo->link, $sql = 'DROP DATABASE IF EXISTS `'.$database.'`') or var_dump($sql);
            mysqli_query($copyTo->link, $sql = $createDatabase['Create Database']) or var_dump($sql);

            mysqli_select_db($this->link, $database);
            mysqli_select_db($copyTo->link, $database);

            $query2 = mysqli_query($this->link, $sql = 'show tables');
            while ($table = mysqli_fetch_assoc($query2)) {
                $table = current($table);
                $createTable = mysqli_fetch_assoc(mysqli_query($this->link, 'SHOW CREATE TABLE `'.$table.'`'));
                mysqli_query($copyTo->link, $sql = $createTable['Create Table']) or var_dump($sql);
                $counts = current(mysqli_fetch_assoc(mysqli_query($this->link, 'SELECT COUNT(*) as counts FROM `'.$table.'`')));
                $range = range(0, $counts - 1);
                $offset = 0;
                $limit = 200;
                $chunks = array_chunk($range, $limit);
                foreach ($chunks as $chunk) {
                    $query3 = mysqli_query($this->link, $sql = 'SELECT * FROM `'.$table.'` LIMIT '.$limit.' OFFSET '.$offset) or var_dump($database, $sql);
                    while ($row = mysqli_fetch_assoc($query3)) {
                        $keys = [];
                        $values = [];
                        foreach ($row as $key => $value) {
                            $keys[] = '`'.$key.'`';
                            $values[] = "'".addslashes($value)."'";
                        }
                        mysqli_query($copyTo->link, $sql = 'INSERT INTO `'.$table.'` ('.implode(',', $keys).') VALUES ('.implode(',', $values).')') or var_dump($database, $sql);
                    }
                    $offset += count($chunk);
                }
            }
        }
    }
}

$database = new DatabaseClone('localhost:3306', 'root', '');
$database->copy(new DatabaseClone('localhost:3308', 'root', ''));