Esempio n. 1
0
 public static function header()
 {
     $str = self::logo();
     $str .= PHP_EOL;
     $str .= Color::getColoredString("Glial 3.2", "green");
     $str .= " (2015-01-05) by Aurélien LEQUOY." . PHP_EOL;
     return $str;
 }
Esempio n. 2
0
 public function __construct($sprite)
 {
     $lines = explode("\n", $sprite);
     foreach ($lines as $line) {
         $len = mb_strlen(Color::strip($line), "utf8");
         if ($len > $this->width) {
             $this->width = $len;
         }
     }
     foreach ($lines as $line) {
         $this->sprite[] = String::str_pad_unicode($line, $this->width);
         $this->height++;
     }
     return $this;
 }
Esempio n. 3
0
/**
 * Prints out debug information about given variable.
 *
 * Only runs if debug level is greater than zero.
 *
 * @param boolean $var Variable to show debug information for.
 * @param boolean $showHtml If set to true, the method prints the debug data in a screen-friendly way.
 * @param boolean $showFrom If set to true, the method prints from where the function was called.
 * @link http://book.cakephp.org/view/1190/Basic-Debugging
 * @link http://book.cakephp.org/view/1128/debug
 */
function debug($var = false, $showHtml = false, $showFrom = true)
{
    if (IS_CLI) {
        $calledFrom = debug_backtrace();
        echo Color::getColoredString(substr(str_replace(ROOT, '', $calledFrom[0]['file']), 1) . ' (line ' . $calledFrom[0]['line'] . ')', "grey", "blue", "bold") . "\n";
        print_r($var);
    } else {
        if (true) {
            if ($showFrom) {
                $calledFrom = debug_backtrace();
                echo '<strong>' . substr(str_replace(ROOT, '', $calledFrom[0]['file']), 1) . '</strong>';
                echo ' (line <strong>' . $calledFrom[0]['line'] . '</strong>)';
            }
            echo "\n<pre>\n";
            print_r($var);
            if ($showHtml) {
                $var = str_replace('<', '&lt;', str_replace('>', '&gt;', $var));
            }
            echo "\n</pre>\n";
        }
    }
}
Esempio n. 4
0
File: Sql.php Progetto: glial/glial
 public final function sql_query($sql, $table = "", $type = "")
 {
     if (IS_CLI) {
         //to save memory with crawler & bot
         $this->serializeQuery();
     }
     if (!is_string($sql)) {
         throw new \Exception('GLI-056 : the var $sql must be a string in sql_query !');
     }
     $this->res = "";
     $this->stid = "";
     $called_from = debug_backtrace();
     $startmtime = microtime(true);
     if (!($res = $this->_query($sql))) {
         $indice = 0;
         if (strstr($called_from[0]['file'], "/Sgbd/Sql/Sql.php")) {
             $indice = 1;
         }
         //error
         if (IS_CLI) {
             echo "SQL : " . Color::getColoredString($sql, "yellow") . "\n" . Color::getColoredString($this->_error(), "grey", "red") . "" . "\nFILE : " . $called_from[$indice]['file'] . " LINE : " . $called_from[$indice]['line'] . "\n";
         } else {
             echo "SQL : {$sql}<br /><b>" . $this->_error() . "</b>" . "<br />FILE : " . $called_from[$indice]['file'] . ":" . $called_from[$indice]['line'] . "<br />";
         }
     }
     $this->res = $res;
     $totaltime = round(microtime(true) - $startmtime, 5);
     $this->query[$this->number_of_query]['query'] = $sql;
     $this->query[$this->number_of_query]['time'] = $totaltime;
     $this->query[$this->number_of_query]['file'] = $called_from[0]['file'];
     $this->query[$this->number_of_query]['line'] = $called_from[0]['line'];
     $this->rows_affected = $this->sql_affected_rows();
     $this->query[$this->number_of_query]['rows'] = $this->rows_affected;
     $this->query[$this->number_of_query]['last_id'] = $this->_insert_id();
     $this->number_of_query++;
     return $res;
 }
Esempio n. 5
0
 public function createAdmin()
 {
     $this->view = false;
     /*
      * email
      * first name
      * last name
      * country
      * city
      * password
      * password repeat
      */
     createUser:
     $this->cadre("create administrator user");
     $email_is_valid = false;
     do {
         $rl = new Hoa\Console\Readline\Readline();
         $email = $rl->readLine('Your email [will be used as login] : ');
         if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
             $this->displayResult("This email considered as valid !", "KO");
         } else {
             $this->displayResult("This email considered as valid !", "OK");
             $domain = explode('@', $email)[1];
             if (checkdnsrr($domain, 'MX')) {
                 $this->displayResult("This MX records exists !", "OK");
                 $email_is_valid = true;
             } else {
                 $this->displayResult("This MX records exists !", "KO");
             }
         }
     } while ($email_is_valid === false);
     //first name
     $firstname = $rl->readLine('Your firstname : ');
     //last name
     $lastname = $rl->readLine('Your lastname : ');
     //country
     $sql = "SELECT libelle FROM geolocalisation_country where libelle != '' ORDER BY libelle";
     $DB = $this->di['db']->sql(DB_DEFAULT);
     $res = $DB->sql_query($sql);
     $country = [];
     while ($ob = $DB->sql_fetch_object($res)) {
         $country[] = $ob->libelle;
     }
     do {
         $rl = new Hoa\Console\Readline\Readline();
         $rl->setAutocompleter(new Hoa\Console\Readline\Autocompleter\Word($country));
         $country2 = $rl->readLine('Your country [First letter in upper case, then tab for help] : ');
         $sql = "select id from geolocalisation_country where libelle = '" . $DB->sql_real_escape_string($country2) . "'";
         $res = $DB->sql_query($sql);
         if ($DB->sql_num_rows($res) == 1) {
             $ob = $DB->sql_fetch_object($res);
             $id_country = $ob->id;
             $this->displayResult("Country found in database !", "OK");
         } else {
             $this->displayResult("Country found in database !", "KO");
         }
     } while ($DB->sql_num_rows($res) != 1);
     //city
     $sql = "SELECT libelle FROM geolocalisation_city where id_geolocalisation_country = '" . $id_country . "' ORDER BY libelle";
     $DB = $this->di['db']->sql(DB_DEFAULT);
     $res = $DB->sql_query($sql);
     $city = [];
     while ($ob = $DB->sql_fetch_object($res)) {
         $city[] = $ob->libelle;
     }
     do {
         $rl = new Hoa\Console\Readline\Readline();
         $rl->setAutocompleter(new Hoa\Console\Readline\Autocompleter\Word($city));
         $city2 = $rl->readLine('Your city [First letter in upper case, then tab for help] : ');
         $sql = "select id from geolocalisation_city where libelle = '" . $DB->sql_real_escape_string($city2) . "'";
         $res = $DB->sql_query($sql);
         if ($DB->sql_num_rows($res) == 1) {
             $ob = $DB->sql_fetch_object($res);
             $id_city = $ob->id;
             $this->displayResult("City found in database !", "OK");
         } else {
             $this->displayResult("City found in database !", "KO");
         }
     } while ($DB->sql_num_rows($res) != 1);
     //password
     $rl = new Hoa\Console\Readline\Password();
     $good = false;
     do {
         $pwd = $rl->readLine('Password : '******'Password (repeat) : ');
         if (!empty($pwd) && $pwd === $pwd2) {
             $good = true;
             $this->displayResult("The passwords must be the same & not empty", "OK");
         } else {
             $this->displayResult("The passwords must be the same & not empty", "KO");
         }
     } while ($good !== true);
     $ip = trim(@file_get_contents("http://icanhazip.com"));
     if (empty($ip) || !filter_var($ip, FILTER_VALIDATE_IP)) {
         $ip = "127.0.0.1";
     }
     $data['user_main']['is_valid'] = 1;
     $data['user_main']['email'] = $email;
     $data['user_main']['login'] = $email;
     $data['user_main']['password'] = \Glial\Auth\Auth::hashPassword($email, $pwd);
     //to set uppercase to composed name like 'Jean-Louis'
     $firstname = str_replace("-", " - ", $firstname);
     $firstname = mb_convert_case($firstname, MB_CASE_TITLE, "UTF-8");
     $data['user_main']['firstname'] = str_replace(" - ", "-", $firstname);
     $data['user_main']['name'] = mb_convert_case($lastname, MB_CASE_UPPER, "UTF-8");
     $data['user_main']['ip'] = $ip;
     $data['user_main']['date_created'] = date('Y-m-d H:i:s');
     $data['user_main']['id_group'] = 4;
     // 4 = super admin
     $data['user_main']['id_geolocalisation_country'] = $id_country;
     $data['user_main']['id_geolocalisation_city'] = $id_city;
     $data['user_main']['id_client'] = 1;
     $id_user = $DB->sql_save($data);
     if ($id_user) {
         $this->displayResult("Admin account successfully created", "OK");
     } else {
         print_r($data);
         $error = $DB->sql_error();
         print_r($error);
         $this->displayResult("Admin account successfully created", "KO");
         goto createUser;
     }
     echo Color::getColoredString("\nAdministrator successfully created !\n", "green");
     $ip_list = shell_exec('ifconfig -a | grep "inet ad" | cut -d ":" -f 2 | cut -d " " -f 1');
     $ips = explode("\n", $ip_list);
     foreach ($ips as $ip) {
         if (empty($ip)) {
             continue;
         }
         echo "You can connect to the application on this url : " . Color::getColoredString("http://" . $ip . WWW_ROOT, "yellow") . "\n";
     }
     echo "You can connect to the application on this url : " . Color::getColoredString("http://" . gethostname() . WWW_ROOT, "yellow") . "\n";
 }
Esempio n. 6
0
 public function clusterTest($param)
 {
     $this->view = false;
     foreach ($param as $server) {
         $db = $this->di['db']->sql(str_replace('-', '_', $server));
         $sql = "CREATE DATABASE IF NOT EXISTS `" . $server . "`;";
         $db->sql_query($sql);
         echo "[" . date("Y-m-d H:i:s") . "] " . $server . "> " . \SqlFormatter::highlight($sql);
     }
     foreach ($param as $server) {
         $db = $this->di['db']->sql(str_replace('-', '_', $server));
         $sql = "SHOW DATABASES";
         $databases = $db->sql_fetch_yield($sql);
         $i = 0;
         foreach ($databases as $database) {
             if (in_array($database['Database'], $param)) {
                 echo "[NOTICE] " . $server . "> " . $database['Database'] . " : Found !" . PHP_EOL;
                 $i++;
             }
         }
         if ($i != count($param)) {
             echo Color::getColoredString("[ERROR] Only {$i} DB found !", "grey", "red") . PHP_EOL;
         } else {
             echo Color::getColoredString("[NOTICE] {$server}> OK !", "black", "green") . PHP_EOL;
         }
     }
     foreach ($param as $server) {
         $db = $this->di['db']->sql(str_replace('-', '_', $server));
         $sql = "DROP DATABASE `" . $server . "`;";
         $db->sql_query($sql);
         echo "[" . date("Y-m-d H:i:s") . "] " . $server . "> " . \SqlFormatter::highlight($sql);
     }
     foreach ($param as $server) {
         $db = $this->di['db']->sql(str_replace('-', '_', $server));
         $sql = "SHOW DATABASES";
         $databases = $db->sql_fetch_yield($sql);
         $i = 0;
         foreach ($databases as $database) {
             if (in_array($database['Database'], $param)) {
                 $i++;
             }
         }
         if ($i != 0) {
             echo Color::getColoredString("[ERROR] {$i} DB found on {$server} !", "grey", "red") . PHP_EOL;
         } else {
             echo Color::getColoredString("[NOTICE] {$server}> No databases found !", "black", "green") . PHP_EOL;
         }
     }
 }
Esempio n. 7
0
 private function removeTableNotImpacted($fks)
 {
     do {
         $tmp = $fks;
         $tmp2 = $fks;
         $nbfound = 0;
         foreach ($fks as $table => $data) {
             //we want keep main table
             if (trim($table) == $this->main_table) {
                 continue;
             }
             $found = false;
             foreach ($tmp2 as $data_to_cmp) {
                 if (in_array($table, $data_to_cmp)) {
                     $found = true;
                 }
             }
             if (!$found) {
                 if ($this->debug) {
                     echo Color::getColoredString("We removed this table (Not a child of : `" . $this->schema_to_purge . "`.`" . $this->main_table . "`) : " . $table, 'black', 'yellow', 'bold') . PHP_EOL;
                 }
                 unset($tmp2[$table]);
                 $nbfound++;
             }
         }
         $fks = $tmp2;
         if ($this->debug) {
             echo str_repeat("-", 80) . "\n";
         }
     } while ($nbfound != 0);
     debug($tmp2);
     die;
     return $tmp2;
 }
Esempio n. 8
0
 public function saveValue($data, $all_status, $id_mysql_server)
 {
     $db = $this->di['db']->sql(DB_DEFAULT);
     $tables = array('mysql_status_value_int', 'mysql_status_value_double', 'mysql_status_value_text');
     $i = 0;
     foreach ($tables as $table) {
         $sql[$i] = "INSERT INTO `" . $table . "` (`id_mysql_server`, `id_mysql_status_name`,`date`, `value`) VALUES ";
         $i++;
     }
     $feed = array();
     $date = date('Y-m-d H:i:s');
     foreach ($all_status as $name => $status) {
         $name = strtolower($name);
         $feed[$data[$name]['type']][] = "(" . $id_mysql_server . "," . $data[$name]['id'] . ",'" . $date . "','" . $status . "')";
     }
     $i = 0;
     foreach ($feed as $tmp) {
         $req = $sql[$i] . implode(',', $tmp) . ";";
         //echo $req."\n";
         try {
             $ret = $db->sql_query($req);
             if (!$ret) {
                 //à changer : chopper l'exception mysql et l'afficher dans le log d'erreur de PmaControl
                 $this->logger->error(Color::getColoredString($ret->sql_error(), "white", "red"));
                 //$this->stop(array(1));
                 //throw new \Exception('PMACTRL-065 : '.$ret->sql_error());
             }
         } catch (Exception $ex) {
             //à changer
             $this->logger->error(Color::getColoredString("ERROR: " . $ex->getMessage(), "white", "red"));
         }
         $i++;
     }
     $db->sql_query("REPLACE INTO mysql_status_max_date  (`id_mysql_server`,`date`) VALUES ('" . $id_mysql_server . "', '" . $date . "');");
 }
Esempio n. 9
0
 public function replication()
 {
     $this->layout_name = false;
     $this->view = false;
     $MS = new MasterSlave();
     $tab = new Table();
     $tab->addHeader(array("Top", "Server", "IP", "Version", "M", "S", "IO", "SQL", "Behind", "File", "Position", "Current file", "Position"));
     $ip = array();
     $masters = array();
     $i = 0;
     foreach ($this->di['db']->getAll() as $db) {
         $i++;
         $server_config = $this->di['db']->sql($db)->getParams();
         $MS->setInstance($this->di['db']->sql($db));
         $master = $MS->isMaster();
         $slave = $MS->isSlave();
         $is_master = $master ? "â– " : "";
         $is_slave = $slave ? "â– " : "";
         $sql = "SHOW GLOBAL VARIABLES LIKE 'version'";
         $res = $this->di['db']->sql($db)->sql_query($sql);
         $data = $this->di['db']->sql($db)->sql_fetch_array($res, MYSQLI_ASSOC);
         if (strpos($data['Value'], "-")) {
             $version = strstr($data['Value'], '-', true);
         } else {
             $version = $data['Value'];
         }
         if ($slave) {
             $io = $slave['Slave_IO_Running'] === 'Yes' ? "OK" : Color::getColoredString("KO", "grey", "red", "bold");
             $sql = $slave['Slave_SQL_Running'] === 'Yes' ? "OK" : Color::getColoredString("KO", "grey", "red", "bold");
             $time_behind = $slave['Seconds_Behind_Master'];
         } else {
             $io = "";
             $sql = "";
             $time_behind = "";
         }
         $master_binlog = $slave ? $slave['Master_Log_File'] : "";
         $master_postion = $slave ? $slave['Exec_Master_Log_Pos'] : "";
         $file = $master ? $master['File'] : "";
         $position = $master ? $master['Position'] : "";
         $addr = !empty($server_config['port']) && is_numeric($server_config['port']) ? $server_config['hostname'] . ":" . $server_config['port'] : $server_config['hostname'];
         $ip[$addr] = array($i, $db, $addr, $version, $is_master, $is_slave, $io, $sql, $time_behind, $file, $position, $master_binlog, $master_postion);
         if ($slave) {
             $masters[$slave['Master_Host']][] = $addr;
         }
     }
     //debug($masters);
     $color = "purple";
     $i = 1;
     $j = 0;
     $colors = array("purple", "yellow", "blue", "cyan", "red", "green");
     $nb_color = count($colors);
     foreach ($masters as $master => $slaves) {
         $color = $colors[$j];
         $j++;
         $j = $j % $nb_color;
         $ip[$master][0] = $i;
         $ip[$master][1] = Color::getColoredString("â–  ", $color) . $ip[$master][1];
         $tab->addLine($ip[$master]);
         unset($ip[$master]);
         foreach ($slaves as $id_slave => $slave) {
             $i++;
             $ip[$slave][0] = $i;
             if ($cpt = count($masters[$master]) == 1) {
                 $ip[$slave][1] = Color::getColoredString("�■ ", $color) . $ip[$slave][1];
             } else {
                 $ip[$slave][1] = Color::getColoredString("�■ ", $color) . $ip[$slave][1];
             }
             $tab->addLine($ip[$slave]);
             unset($masters[$master][$id_slave]);
             unset($ip[$slave]);
         }
         $i++;
     }
     foreach ($ip as $server) {
         $server[1] = "â–  " . $server[1];
         $server[0] = $i;
         $tab->addLine($server);
         $i++;
     }
     /*
     *             $tab->addLine(array($i, $db, $addr, $version, $is_master, $is_slave, $io, $sql, $time_behind,
      $file, $position, $master_binlog, $master_postion, "Log space"));
     */
     /* $tab->addLine(array($server_name, $server_config['hostname'], $version, $is_master, $is_slave, $io, $sql, $time_behind,
         $file, $position, $master_binlog,$master_postion, "Log space"));
        */
     echo PHP_EOL . $tab->display();
     //echo Color::printAll();
 }
Esempio n. 10
0
File: Acl.php Progetto: glial/glial
 public function __toString()
 {
     if (IS_CLI) {
         $number_length = ceil(log(count($this->resources), 10));
         $cli = "┌" . str_repeat("─", 2 + $this->maxLength['ressource'] + $number_length) . str_repeat("┬─", count($this->roles)) . "┐\n";
         ksort($this->resources);
         ksort($this->roles);
         $tab_role = array();
         foreach ($this->roles as $role => $var) {
             $tab_role[] = str_split(str_repeat(" ", $this->maxLength['role'] - mb_strlen($role)) . $role);
         }
         for ($i = 0; $i < $this->maxLength['role']; $i++) {
             $cli .= "│ " . str_repeat(" ", $number_length + $this->maxLength['ressource']) . " ";
             foreach ($tab_role as $role) {
                 $cli .= "│" . Color::getColoredString($role[$i], "grey");
             }
             $cli .= "│\n";
         }
         $cli .= "├" . str_repeat("─", $number_length + 2 + $this->maxLength['ressource']) . str_repeat("┼─", count($this->roles)) . "┤\n";
         $i = 1;
         foreach ($this->resources as $resource => $parent) {
             $background = ($i - 1) % 3 === 0 ? "blue" : "black";
             $cli .= Color::getColoredString("│", null, $background) . Color::getColoredString(str_repeat(" ", $number_length - strlen($i)) . $i, null, $background) . Color::getColoredString(" " . $resource, null, $background) . Color::getColoredString(str_repeat(" ", $this->maxLength['ressource'] - mb_strlen($resource)), null, $background) . Color::getColoredString(" ", null, $background);
             foreach ($this->roles as $role => $var) {
                 $cli .= $this->isAllowed($role, $resource) ? Color::getColoredString("│■", null, $background) : Color::getColoredString("│ ", null, $background);
             }
             $cli .= Color::getColoredString("│", null, $background) . "\n";
             $i++;
         }
         $cli .= "└" . str_repeat("─", $number_length + 2 + $this->maxLength['ressource']) . str_repeat("┴─", count($this->roles)) . "┘\n";
         return $cli;
     } else {
         $html = '<ul><h1>Available Roles</h1>';
         foreach ($this->roles as $role => $parents) {
             $html .= '<li>' . $role . '<br />';
             foreach ($parents as $parent) {
                 $html .= '<i>inherits</i>  ' . $parent . '</li>';
             }
             $html .= '</li>';
         }
         $html .= '</ul><ul><h1>Available Resources</h1>';
         foreach ($this->resources as $resource => $parent) {
             $html .= '<li>' . $resource . '</li>';
         }
         $html .= '</ul><ul><h1>Allow / Deny</h1>';
         foreach ($this->access as $group => $tab_ressource) {
             foreach ($tab_ressource as $ressource => $allowed) {
                 $html .= '<li>' . $group . '+' . $ressource . ': ' . $allowed . '</li>';
             }
         }
         return $html . '</ul>';
     }
 }
Esempio n. 11
0
 private function encapsulate()
 {
     $out = "";
     $top = 1;
     debug($this);
     $lines = explode("\n", $this->windows);
     $before_after = ($this->max_height - count($lines)) / 2;
     $this->after = $before_after;
     for ($i = 0; $i < floor($before_after); $i++) {
         $out .= Color::getColoredString(str_pad($this->cursor_position_vertical . ":" . $top, 5), "grey", $this->color_background);
         $out .= Color::getColoredString(str_repeat(" ", $this->max_width - 5), $this->color_shadow, $this->color_background);
         $out .= $this->eol();
         $top++;
     }
     $before = ($this->max_width - $this->msg_width) / 2;
     $this->before = $before;
     foreach ($lines as $line) {
         $out .= Color::getColoredString(str_pad($this->cursor_position_vertical . ":" . $top, 5), "grey", $this->color_background);
         $out .= Color::getColoredString(str_repeat(" ", $before - 5), $this->color_shadow, $this->color_background);
         $out .= $line;
         $out .= Color::getColoredString(str_repeat(" ", ceil($before)), $this->color_shadow, $this->color_background);
         $out .= $this->eol();
         $top++;
     }
     for ($i = $this->cursor_position_vertical; $i <= $this->max_height; $i++) {
         //echo $i.":";
         $out .= Color::getColoredString(str_pad($this->cursor_position_vertical . ":" . $top . ":" . $i . ":" . $this->max_height, 11), "grey", $this->color_background);
         $out .= Color::getColoredString(str_repeat(" ", $this->max_width - 11), $this->color_shadow, $this->color_background);
         if ($i != $this->max_height) {
             $out .= $this->eol();
         }
         $top++;
     }
     return $out;
     //echo $this->cursor_position_vertical;
 }
Esempio n. 12
0
 public function onError()
 {
     echo PHP_EOL . "To understand what happen : " . Color::getColoredString($_SERVER['PWD'] . "/tmp/log/error_php.log", "cyan") . PHP_EOL;
     echo "To resume the setup : " . Color::getColoredString("php composer.phar update", "cyan") . PHP_EOL;
     exit(10);
 }
Esempio n. 13
0
 public function setPasswdPmaControl()
 {
     $this->view = false;
     do {
         fwrite(STDOUT, "Choose password for remote user pmacontrol\n");
         $passwdpma = self::promptSilent("Enter new UNIX password:"******"Retype new UNIX password:"******"Sorry, passwords do not match\n\n", "red");
         }
     } while ($passwdpma != $passwdpma2);
     Crypt::$key = CRYPT_KEY;
     $password_crypted = Crypt::encrypt($passwdpma);
     $file = "<?php\r\nif (! defined('PMACONTROL_PASSWD'))\r\n{\r\n    define('PMACONTROL_PASSWD', '" . $password_crypted . "');\r\n}\r\n";
     file_put_contents(CONFIG . "pmacontrol.config.php", $file);
     return $passwdpma;
 }