helpers.php 3.3 KB
Newer Older
Juliper committed
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 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
<?php

if (!function_exists('command_exists')) {
    /**
     * Check if command exists.
     *
     * @param $command
     * @return boolean
     */
    function command_exists($command)
    {
        exec('type "'. $command . '"',$result,$return);
        return !$return;
    }
}

if (!function_exists('validate_email')) {
    /**
     * Validate email
     * @param $email
     * @return null
     */
    function validate_email($email)
    {
        if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
            return $email;
        }
        return null;
    }
}

if (!function_exists('git_user_email')) {
    /**
     * Get user email from git config.
     */
    function git_user_email()
    {
        if (!command_exists("git")) return null;
        $result = exec('git config user.email');

        $email = validate_email($result);
        if (!is_null($email)) return $email;

        $result = exec('git config --global user.email');
        return validate_email($result);
    }
}

if (!function_exists('git_user_name')) {
    /**
     * Get user email from git config.
     */
    function git_user_name()
    {
        if (!command_exists("git")) return null;
        $result = exec('git config user.name');

        if ($result != "") return $result;

        $result = exec('git config --global user.name');
        if ($result != "") return $result;
        return null;
    }
}

if (!function_exists('scape_single_quotes')) {
    /**
     * Scape single quotes for sed using \x27.
     *
     * @param string $str
     *
     * @return string
     */
    function scape_single_quotes($str)
    {
        return str_replace("'", '\\x27', $str);
    }
}


if (!function_exists('add_text_into_file')) {
    /**
     * Insert text into file using mountpoint and sed command. Mountpoint is maintained at file for future uses.
     *
     * @param $mountpoint
     * @param $textToAdd
     * @param $file
     * @return mixed
     */
    function add_text_into_file($mountpoint, $textToAdd, $file)
    {
        passthru(
            'sed -i \'s/.*'.$mountpoint.'.*/ \ \ \ \ \ \ \ '. scape_single_quotes(preg_quote($textToAdd)).',\n \ \ \ \ \ \ \ '.$mountpoint.'/\' '. $file, $error);

        return $error;
    }
}

if (!function_exists('add_file_into_file')) {
    /**
     * Insert file into file using mountpoint.
     *
     * @param $mountpoint
     * @param $fileToInsert
     * @param $file
     * @param null $outputFile
     * @return mixed
     */
    function add_file_into_file($mountpoint, $fileToInsert,$file, $outputFile = null)
    {
        if ($outputFile != null) {
            passthru(
                'sed -e \'/'.$mountpoint.'/r'.$fileToInsert.'\' '.
                $file.' > '.$outputFile, $error);
        } else {
            passthru(
                'sed -i \'/'.$mountpoint.'/r'.$fileToInsert.'\' '.$file, $error);
        }

        return $error;
    }
}

if (!function_exists('dot_path')) {
    /**
     * Converts regular path to dotted path.
     *
     * @param $path
     * @return mixed
     */
    function dot_path($path)
    {
        return str_replace("/", ".", $path);
    }
}

if (!function_exists('undot_path')) {
    /**
     * Converts dotted path to regular path.
     *
     * @param $path
     * @return mixed
     */
    function undot_path($path)
    {
        return str_replace(".", "/", $path);
    }
}