示例#1
0
 function process()
 {
     Console::initCore();
     if ($r = ArgsHolder::get()->getOption('count')) {
         $this->count = $r;
     }
     if (($c = ArgsHolder::get()->shiftCommand()) == 'help') {
         return $this->cmdHelp();
     }
     try {
         IO::out("");
         $sql = 'SELECT * FROM ' . self::TABLE . ' where not isnull(finished_at) and not 
             isnull(locked_at) and isnull(failed_at) ORDER BY run_at DESC';
         if ($this->count) {
             $list = DB::query($sql . ' LIMIT ' . $this->count);
         } else {
             $list = DB::query($sql);
         }
         if (!count($list)) {
             IO::out("No finished work!", IO::MESSAGE_FAIL);
             return;
         }
         io::out(sprintf("%-10s %-7s %-3s %-20s %-20s %-19s %-4s %-5s", "~CYAN~id", "queue", "pr", "run_at", "locked_at", "finished_at", "att", "call_to~~~"));
         foreach ($list as $l) {
             $handler = unserialize($l["handler"]);
             io::out(sprintf("%-4s %-7s %-3s %-20s %-20s %-20s %-3s %-5s", $l["id"], $l["queue"], $l["priority"], $l["run_at"], $l["locked_at"], $l["finished_at"], $l["attempts"], $handler["class"] . "::" . $handler["method"] . "(...)"));
         }
     } catch (Exception $e) {
         io::out($e->getMessage(), IO::MESSAGE_FAIL);
         return;
     }
     IO::out("");
 }
示例#2
0
 function cmdUpdate()
 {
     io::out('~WHITE~Updating repositories:~~~');
     $list = array();
     $width = 0;
     if (($url = ArgsHolder::get()->shiftCommand()) != false) {
         $list = array($url);
     } else {
         $list = $this->rl->getList();
     }
     if (!count($list)) {
         return io::out('Repository list empty', IO::MESSAGE_WARN) | 2;
     }
     foreach ($list as $url) {
         $width = max($width, strlen($url));
     }
     foreach ($this->rl->getList() as $url) {
         io::out(sprintf('%-' . ($width + 2) . 's', $url), false);
         try {
             $r = $this->rl->updateRepository($url);
             if ($r === false) {
                 io::out('[ ~GRAY~not modified~~~ ]');
             } else {
                 io::out('[ ' . sprintf('%2u', $r) . ' packages ]');
             }
         } catch (RepositoryListException $e) {
             io::out($e->getMessage(), IO::MESSAGE_FAIL);
         }
     }
 }
示例#3
0
 function process()
 {
     Console::initCore();
     if (($c = ArgsHolder::get()->shiftCommand()) == 'help') {
         return $this->cmdHelp();
     }
     try {
         $format = "%-25s %s";
         IO::out("");
         $s = DB::query('SELECT * FROM ' . self::TABLE . ' where id="' . $c . '"');
         if (!count($s)) {
             io::out("Work with id={$c} is not exists", IO::MESSAGE_FAIL);
             return;
         }
         io::out(sprintf($format, "~CYAN~id~~~", $c));
         io::out(sprintf($format, "~CYAN~queue~~~", $this->emp($s[0]['queue'])));
         io::out(sprintf($format, "~CYAN~priority~~~", $this->emp($s[0]['priority'])));
         io::out(sprintf($format, "~CYAN~run at~~~", $this->emp($s[0]['run_at'])));
         io::out(sprintf($format, "~CYAN~locked at~~~", $this->emp($s[0]['locked_at'])));
         io::out(sprintf($format, "~CYAN~finished at~~~", $this->emp($s[0]['finished_at'])));
         io::out(sprintf($format, "~CYAN~failed at~~~", $this->emp($s[0]['failed_at'])));
         io::out(sprintf($format, "~CYAN~attemts~~~", $this->emp($s[0]['attempts'])));
         $handler = unserialize($s[0]["handler"]);
         io::out(sprintf($format, "~CYAN~call~~~", $handler["class"] . "::" . $handler["method"]) . "(...)");
         if (isset($handler["param"])) {
             io::out(sprintf($format, "~CYAN~params~~~", trim(self::walker($handler['param']), ',')));
         }
     } catch (Exception $e) {
         io::out($e->getMessage(), IO::MESSAGE_FAIL);
         return;
     }
     IO::out("");
 }
示例#4
0
 public function cmdExt()
 {
     if ($ext = ArgsHolder::get()->shiftCommand()) {
         return $this->infoByExt($ext);
     } else {
         return $this->allExtInfo();
     }
 }
示例#5
0
 function process()
 {
     Console::initCore();
     if (($c = ArgsHolder::get()->shiftCommand()) == 'help') {
         return $this->cmdHelp();
     }
     try {
         if (is_numeric($c)) {
             $this->deleteById($c);
         } else {
             $this->deleteQueue($c);
         }
     } catch (Exception $e) {
         io::out($e->getMessage(), IO::MESSAGE_FAIL);
         return;
     }
 }
示例#6
0
 function FileUpadter()
 {
     FileSystemObject::setRoot(dirname(__FILE__));
     $d = new Dir('files');
     $fu = new FileUpdater($d->getDir('source/includes'), $d->getDir('target/includes'), $d->getDir('backup/includes'));
     $command = ArgsHolder::get()->shiftCommand();
     if ($command == 'cdo') {
         $fu->copyDo();
     } elseif ($command == 'cundo') {
         $fu->copyUndo();
     } elseif ($command == 'undo') {
         $fu->undo();
     } elseif ($command == 'do') {
         $fu->run();
     } else {
         io::out('command not found');
     }
 }
示例#7
0
 function process()
 {
     Console::initCore();
     if (($c = ArgsHolder::get()->shiftCommand()) == 'help') {
         return $this->cmdHelp();
     }
     try {
         if (is_numeric($c)) {
             $this->RestartById($c);
         } else {
             io::out("You can input only id of work.", IO::MESSAGE_FAIL);
         }
         return;
     } catch (Exception $e) {
         io::out($e->getMessage(), IO::MESSAGE_FAIL);
         return;
     }
 }
示例#8
0
 function process()
 {
     if (!($c = ArgsHolder::get()->shiftCommand())) {
         return io::out('Incorrect parameter', IO::MESSAGE_FAIL) | 1;
     }
     $root = Config::get('ROOT_DIR');
     $file = $root . '/includes/env/' . strtolower($c) . '_env.php';
     if (!file_exists($file)) {
         return io::out('Mode ' . $c . ' not exists', IO::MESSAGE_FAIL) | 1;
     }
     IO::out('Updating Loader', false);
     $loader = fopen($root . '/includes/env/Loader.php', 'w');
     flock($loader, LOCK_EX);
     $put = '<?php require_once("' . $c . '_env.php");';
     fwrite($loader, $put);
     flock($loader, LOCK_UN);
     fclose($loader);
     io::done();
     io::out('Backup config.ini', false);
     if (copy($root . '/config/config.ini', $root . '/config/config.ini.bak')) {
         io::done();
     } else {
         return IO::out('Can\'t backup file config.ini', IO::MESSAGE_FAIL) | 1;
     }
     IO::out('Updating config.ini', false);
     $file_array = file($root . '/config/config.ini');
     $str = null;
     foreach ($file_array as $fa) {
         if (preg_match('/\\[\\s*config\\s*:\\s*\\S+\\]/', $fa, $match)) {
             $str .= '[config : ' . $c . ']' . PHP_EOL;
         } else {
             $str .= $fa;
         }
     }
     $config = fopen($root . '/config/config.ini', 'w');
     flock($config, LOCK_EX);
     fwrite($config, $str);
     flock($config, LOCK_UN);
     fclose($config);
     io::done();
     IO::done('Enviroments set to ~WHITE~' . $c . '~~~');
 }
示例#9
0
文件: CmdGroup.php 项目: point/cassea
 public function cmdUserlist()
 {
     if (!count(ACL::getGroups())) {
         return io::out('There is no groups yet.');
     }
     $users = ACL::getUsers();
     if ($group = ArgsHolder::get()->shiftCommand()) {
         if (!in_array($group, array_values(ACL::getGroups()))) {
             return io::out("No such group {$group}", IO::MESSAGE_FAIL) | 3;
         }
         IO::out("~WHITE~User(s) from group " . $group . "~~~:");
         foreach ($users[$group] as $u) {
             IO::out(" " . $u);
         }
     } else {
         foreach (array_keys($users) as $g) {
             IO::out("~WHITE~Group " . $g . "~~~:");
             foreach ($users[$g] as $u) {
                 IO::out(" " . $u);
             }
         }
     }
 }
示例#10
0
文件: CmdClone.php 项目: point/cassea
 function process()
 {
     $root = Config::get('ROOT_DIR');
     if (($src = ArgsHolder::get()->shiftCommand()) === false || ($new = ArgsHolder::get()->shiftCommand()) === false) {
         return io::out('Incorrect param count', IO::MESSAGE_FAIL) | 1;
     }
     $src_file = $root . '/includes/env/' . strtolower($src) . '_env.php';
     $new_file = $root . '/includes/env/' . strtolower($new) . '_env.php';
     if (file_exists($new_file)) {
         return io::out('Enviroments ~WHITE~' . $new . '~~~ already exists!', IO::MESSAGE_FAIL) | 2;
     }
     if (!file_exists($src_file)) {
         return io::out('Source enviroments ~WHITE~' . $src . '~~~ is not exists!', IO::MESSAGE_FAIL) | 2;
     }
     io::out('Writing files', false);
     if (!copy($src_file, $new_file)) {
         return io::out('Unable copy env files (' . $src_file . ' to ' . $new_file . ')', IO::MESSAGE_FAIL) | 3;
     }
     $file_array = file($root . '/config/config.ini');
     $str = null;
     foreach ($file_array as $fa) {
         if (preg_match('/\\[\\s*config\\s*:\\s*\\S+\\]/', $fa, $match)) {
             $str .= '[' . $new . ': base ]' . PHP_EOL . $match[0] . PHP_EOL;
         } else {
             $str .= $fa;
         }
     }
     $config = fopen($root . '/config/config.ini', 'w');
     flock($config, LOCK_EX);
     $r = fwrite($config, $str);
     flock($config, LOCK_UN);
     fclose($config);
     if (!$r) {
         return io::out('Cant write ~WHITE~config.ini~~~', IO::MESSAGE_FAIL) | 4;
     }
     io::done();
 }
示例#11
0
 /**
  * Удаление указанного пакета
  */
 function cmdUninstall()
 {
     if (($nvr = ArgsHolder::get()->shiftCommand(false)) == false) {
         return IO::out('Specify package file', IO::MESSAGE_FAIL) | 1;
     }
     try {
         PackageManager::get()->startup();
         PackageManager::uninstall(trim($nvr, '\'"'));
         PackageManager::get()->shutdown();
     } catch (Exception $e) {
         PackageManager::getRollback()->stepBack();
         return io::out($e->getMessage(), IO::MESSAGE_FAIL) | (is_null($e->getCode()) ? 2 : $e->getCode());
     }
 }
示例#12
0
 /**
  * Реализация singleton
  *
  * @return ArgsHolder
  */
 static function get()
 {
     if (!is_object(self::$instance)) {
         self::$instance = new ArgsHolder();
     }
     return self::$instance;
 }
示例#13
0
 public function cmdRemove()
 {
     if (($name = ArgsHolder::get()->shiftCommand()) === false) {
         return io::out('Incorrect param count', IO::MESSAGE_FAIL);
     }
     if (file_exists($this->root_dir . '/controllers/' . $name . '.php')) {
         if (IO::YES == io::dialog('Realy Delete controller,models,pages with name ~WHITE~' . $name . '~~~?', IO::NO | IO::YES, IO::NO)) {
             IO::out('~WHITE~Removing:~~~');
             self::rRem($this->root_dir . '/controllers/' . $name . '.php');
             io::done('     controllers/' . $name . '.php');
             self::rRem($this->root_dir . $this->models_dir . '/' . $name);
             io::done('     ' . $this->models_dir . '/' . $name);
             self::rRem($this->root_dir . '/pages/' . $name);
             io::done('     pages/' . $name);
         }
     } else {
         io::out('Controller with name ~WHITE~' . $name . '~~~ not exist ', IO::MESSAGE_FAIL);
     }
 }
示例#14
0
文件: Command.php 项目: point/cassea
 /**
  * Обработка подкомманд
  *
  * Создает объект комманды, инициализирует его и вызывает метод process()
  *
  * @param string $cmd имя подкомманды
  * @return int результат работы подкомманды
  */
 private function processSubcommands($cmd)
 {
     //IO::out('~RED~ process Subcommand :'.$cmd.'~~~');
     $info = $this->getCommandInfo($cmd);
     $cmdRoot = $this->root . 'commands/' . $cmd . '/';
     //IO::out('cmdRoot: '.$cmdRoot);
     $cmdPath = $cmdRoot . $info['class'] . '.php';
     //IO::out('cmdPath: '.$cmdPath);
     //IO::out('info');
     //io::out();
     if (!is_file($cmdPath)) {
         throw new ConsoleException('Cant find command file "' . $cmdPath . '" for command "' . $cmd . '"');
     }
     require_once $cmdPath;
     try {
         //IO::out($info['class'].' Process');
         $cmd = new $info['class']($cmdRoot, $info, $this->commandsSeq);
         if (ArgsHolder::get()->isHelp()) {
             $cmd->cmdHelp();
         } else {
             return $cmd->process();
         }
     } catch (Exception $e) {
         call_user_func(array($info['class'], 'processException'), $e, $info['class']);
     }
 }
示例#15
0
 public function process()
 {
     Console::initCore();
     $this->separate = ArgsHolder::get()->getOption('separate');
     $this->all = ArgsHolder::get()->getOption('all');
     if ($r = ArgsHolder::get()->getOption('send-email')) {
         $this->send_email = true;
         $this->email_address = explode(",", $r);
         foreach ($this->email_address as $emails) {
             if (!preg_match(POSTChecker::$email_regexp, $emails)) {
                 io::out("Incorrect email address format", IO::MESSAGE_FAIL);
                 return;
             }
         }
     }
     try {
         $root = Config::get('ROOT_DIR');
         $name = basename($root);
         //Create MySQL dump
         $sqlname = $this->createMySQL($root, $name, $this->separate);
         //Create backup file
         $filename = $this->createTar($root, $name, $this->separate);
         if (!$this->separate) {
             unlink($root . '/' . $sqlname);
         }
         if ($this->send_email) {
             if (is_array($this->email_address)) {
                 $toout = implode(',', $this->email_address);
             } else {
                 $toout = $this->email_address;
             }
             io::out("Sending email to ~WHITE~" . $toout . '~~~', false);
             $a = Mail::CreateMail();
             foreach ($this->email_address as $emails) {
                 $a->toAdd($emails);
             }
             $a->setSubject("Backup " . $filename);
             $a->setFromname(Config::getInstance()->mail->default_from_name);
             $a->setFrom(Config::getInstance()->mail->default_from);
             $a->Message("Backup with filename " . $filename);
             $a->attachAdd($root . "/" . $filename);
             if ($this->separate) {
                 $a->attachAdd($root . "/" . $sqlname);
             }
             $r = $a->send();
             if ($r === false) {
                 io::out('', IO::MESSAGE_FAIL);
             } else {
                 io::done();
                 if (ArgsHolder::get()->getOption('clean')) {
                     IO::out('Cleaning ', false);
                     if ($this->separate) {
                         IO::out($sqlname . ' ', false);
                         unlink($root . '/' . $sqlname);
                     }
                     IO::out($filename . ' ', false);
                     unlink($root . '/' . $filename);
                     IO::done();
                 }
             }
         }
     } catch (Exception $e) {
         io::out($e->getMessage(), IO::MESSAGE_FAIL);
         return;
     }
 }
示例#16
0
文件: CmdUser.php 项目: point/cassea
 public function cmdPassword()
 {
     try {
         $login = ArgsHolder::get()->shiftCommand();
         $password = ArgsHolder::get()->shiftCommand();
         if ($login === false) {
             return io::out('Incorrect param count', IO::MESSAGE_FAIL) | 1;
         }
         if ($user = User::findBy("login", $login)) {
             if (!$password) {
                 IO::out('New password: '******'Confirm New password: '******'Passwords not match.', IO::MESSAGE_FAIL) | 2;
                 }
             }
             $user->setPassword($password);
         } else {
             return io::out(PHP_EOL . 'User ~WHITE~' . $login . '~~~ not found', IO::MESSAGE_FAIL) | 3;
         }
     } catch (UserException $e) {
         return io::out($e->getMessage(), IO::MESSAGE_FAIL) | 127;
     }
 }
示例#17
0
文件: CmdCheck.php 项目: point/cassea
 function process()
 {
     $root = Config::get('ROOT_DIR');
     $file_array = file_get_contents($root . '/config/config.ini');
     try {
         //Look for php-cgi in the system
         $php_path = exec("which php-cgi");
         if (empty($php_path) || !is_executable($php_path)) {
             IO::out('Look for PHP-CGI in the system.', IO::MESSAGE_FAIL);
             IO::out("You can't to check hosting because PHP-CGI not found.");
             return;
         }
         $out = exec("echo \"<?php echo 'php-cgi test' ?>\" | {$php_path} -q ");
         if ($out != "php-cgi test") {
             IO::out('Look for PHP-CGI in the system.', IO::MESSAGE_FAIL);
             IO::out("You can't to check hosting because PHP-CGI not found.");
             return;
         }
         IO::out('Look for PHP-CGI in the system.', IO::MESSAGE_OK);
         $toini = array();
         foreach ($this->for_check as $k => $v) {
             $output = array();
             $subcmd = '<?php $r=ini_get("' . $k . '");if($r) echo $r; else echo ((int)$r);';
             exec("echo '" . $subcmd . "' | {$php_path} -q", $output);
             if ($k == 'error_log') {
                 IO::out($k . ' = ' . $output[0], IO::MESSAGE_OK);
                 continue;
             }
             if ($k == 'error_log' || $k == 'max_execution_time' || $k == 'max_input_time' || $k == 'upload_max_filesize' || $k == 'memory_limit' || $k == 'post_max_size') {
                 $ch = str_replace('m', '', strtolower($output[0]));
                 if ((int) $ch >= (int) $v) {
                     IO::out($k . ' = ' . $output[0], IO::MESSAGE_OK);
                     continue;
                 } else {
                     io::out($k . ' = ' . $output[0] . ' this must be >=' . $v, IO::MESSAGE_FAIL);
                     continue;
                 }
             }
             if ($v == $output[0]) {
                 IO::out($k . ' = ' . $output[0], IO::MESSAGE_OK);
             } else {
                 $out = array();
                 $subcmd = '<?php ini_set("' . $k . '","' . $v . '");$r=ini_get("' . $k . '");if($r=="' . $v . '") echo 111; else echo print_r($r);';
                 exec("echo '" . $subcmd . "' | {$php_path} -q", $out);
                 if ($out[0] == 111) {
                     $toini[$k] = $v;
                     IO::out($k . ' = ' . $output[0] . ', but you can set "' . $v . '"', IO::MESSAGE_WARN);
                 } else {
                     io::out($k . ' = ' . $output[0], IO::MESSAGE_FAIL);
                 }
             }
         }
         $output = array();
         $subcmd = '<?php $r=ini_get("upload_max_filesize");if($r) echo $r; else echo ((int)$r);';
         exec("echo '" . $subcmd . "' | {$php_path} -q", $output);
         $umf = str_replace('m', '', strtolower($output[0]));
         $output = array();
         $subcmd = '<?php $r=ini_get("post_max_size");if($r) echo $r; else echo ((int)$r);';
         exec("echo '" . $subcmd . "' | {$php_path} -q", $output);
         $pms = str_replace('m', '', strtolower($output[0]));
         $output = array();
         $subcmd = '<?php $r=ini_get("memory_limit");if($r) echo $r; else echo ((int)$r);';
         exec("echo '" . $subcmd . "' | {$php_path} -q", $output);
         $ml = str_replace('m', '', strtolower($output[0]));
         if ((int) $pms < (int) $umf) {
             IO::out('post_max_size < upload_max_filesize', IO::MESSAGE_FAIL);
         }
         if ((int) $pms > (int) $ml) {
             IO::out('post_max_size > memory_limit', IO::MESSAGE_FAIL);
         }
         if ((int) $pms >= (int) $umf && (int) $pms < (int) $ml) {
             IO::out('upload_max_filesize(' . $umf . ')=< ini.post_max_size(' . $pms . ') < ini.memory_limit(' . $ml . ')', IO::MESSAGE_OK);
         }
         if (is_writable('/tmp')) {
             IO::out('Temp dir is writable.', IO::MESSAGE_OK);
         } else {
             IO::out('Temp dir is not writable!', IO::MESSAGE_FAIL);
         }
         IO::out();
         IO::out('Check for extensions:');
         exec("{$php_path} -m", $output);
         foreach ($this->ext as $e) {
             if ($e == 'gd') {
                 if (!in_array($e, $output)) {
                     if (!in_array('imagick', $output)) {
                         io::out('Extension ' . $e . ' or imagick are not loaded.', IO::MESSAGE_FAIL);
                     } else {
                         io::out('Extension imagick is loaded.', IO::MESSAGE_OK);
                     }
                 } else {
                     io::out('Extension ' . $e . ' is loaded.', IO::MESSAGE_OK);
                 }
                 continue;
             }
             if (!in_array($e, $output)) {
                 io::out('Extension ' . $e . ' is not loaded.', IO::MESSAGE_FAIL);
             } else {
                 if ($e == 'apc') {
                     io::out('Extension ' . $e . ' is loaded.You can set "apc.enabled=On" in php.ini.', IO::MESSAGE_OK);
                     continue;
                 } else {
                     io::out('Extension ' . $e . ' is loaded.', IO::MESSAGE_OK);
                 }
             }
         }
         IO::out();
         IO::out('Check for modes:');
         IO::out('Check for Production:');
         foreach ($this->mod_production as $k => $v) {
             $output = array();
             $subcmd = '<?php ini_set("' . $k . '",' . (int) $v . ');$r=ini_get("' . $k . '");if($r==' . $v . ') echo 1; else echo (int)$r;';
             exec("echo '" . $subcmd . "' | {$php_path} -q", $output);
             if ($output[0]) {
                 IO::out($k . ' = ' . $v, IO::MESSAGE_OK);
             } else {
                 IO::out($k . ' = ' . $output[0], IO::MESSAGE_FAIL);
             }
         }
         IO::out();
         IO::out('Check for Development:');
         foreach ($this->mod_dev as $k => $v) {
             $output = array();
             $subcmd = '<?php ini_set("' . $k . '",' . (int) $v . ');$r=ini_get("' . $k . '");if($r==' . $v . ') echo 1; else echo (int)$r;';
             exec("echo '" . $subcmd . "' | {$php_path} -q", $output);
             if ($output[0]) {
                 IO::out($k . ' = ' . $v, IO::MESSAGE_OK);
             } else {
                 IO::out($k . ' = ' . $output[0], IO::MESSAGE_FAIL);
             }
         }
         IO::out();
         if (ArgsHolder::get()->getOption('generate-ini')) {
             if (!empty($toini)) {
                 io::out('Generating php.ini', false);
                 $res = array();
                 if (file_exists(Config::get('ROOT_DIR') . '/php.ini')) {
                     $file = file(Config::get('ROOT_DIR') . '/php.ini');
                     for ($i = 0; $i < count($file); $i++) {
                         if ($file[$i] != "\n") {
                             $res[strtok($file[$i], '=')] = strtok('=');
                         }
                     }
                 }
                 foreach ($toini as $k => $v) {
                     if (!isset($res[$k])) {
                         $res[$k] = $v;
                     }
                     if ($res[$k] != $toini[$k]) {
                         $res[$k] = $toini[$k];
                     }
                 }
                 foreach ($res as $key => $val) {
                     $write[] = $key . '=' . $val;
                 }
                 $f = fopen(Config::get('ROOT_DIR') . '/php.ini', 'w');
                 if (flock($f, LOCK_EX)) {
                     fwrite($f, implode("\n", $write));
                     flock($f, LOCK_UN);
                 } else {
                     echo "Couldn't get the lock!";
                 }
                 fclose($f);
                 IO::done();
             }
         }
     } catch (exception $e) {
         io::out($e->getmessage(), IO::MESSAGE_FAIL);
         return;
     }
 }
示例#18
0
文件: Console.php 项目: point/cassea
 /**
  * Выполняет первую комманду из коммандной строки.
  *
  * Если комманды не указанны то выводится информационное сообщение.
  */
 public function process()
 {
     if (!count(ArgsHolder::get()->getCommands())) {
         return IO::out('Type "~WHITE~' . Console::CMD . ' help"~~~ for usage.');
     }
     $cmdConsole = new Command(dirname(dirname(__FILE__)) . '/', $this->cmdConsoleInfo);
     return $cmdConsole->process();
 }
示例#19
0
文件: IO.php 项目: point/cassea
 /**
  * Обрабатывает опции и устанавливает {@link $verboseLevel}, {@link $useColor} и {@link $assumeYes}.
  *
  * @param ArgsHolder $ah параметрый коммандной строки. 
  */
 static function init(ArgsHolder $ah)
 {
     if ($ah->getOption('v')) {
         self::$verboseLevel = IO::MESSAGE_INFO;
     }
     if ($ah->getOption('q')) {
         self::$verboseLevel = IO::MESSAGE_FAIL;
     }
     if ($ah->getOption('C')) {
         self::$useColor = false;
     }
     if ($ah->getOption('y') || self::$verboseLevel == IO::MESSAGE_FAIL) {
         self::$assumeYes = true;
     }
 }
示例#20
0
 public function process()
 {
     Console::initCore();
     $version = array();
     exec('tidy -v 2>&1', $version, $ret);
     $version = implode("\n", $version);
     if ($ret != 0) {
         return io::out("You should install tidy to use converter. Exiting.") | 1;
     }
     $ah = ArgsHolder::get();
     $this->show_body_only = $ah->getOption('show-body-only');
     $this->tidy_only = $ah->getOption('tidy-only');
     if ($f = ArgsHolder::get()->getOption('output')) {
         $this->output_file = trim($f, "/");
     }
     if (($filename = ArgsHolder::get()->shiftCommand()) === false) {
         $this->cmdHelp();
         return io::out("Choose file to convert. Exiting.", IO::MESSAGE_FAIL) | 2;
     }
     if (!file_exists($filename)) {
         $filename = getcwd() . "/" . trim($filename, "/");
     }
     touch($filename);
     if (!file_exists($filename)) {
         return io::out("File " . $filename . " not found", IO::MESSAGE_FAIL) | 2;
     }
     if (!empty($this->output_file)) {
         if (dirname($this->output_file) == "") {
             $this->output_file = getcwd() . "/" . $this->output_file;
         }
         if (!is_dir(dirname($this->output_file))) {
             return io::out("Output direcotory doesn't exists", IO::MESSAGE_FAIL) | 4;
         }
         touch($this->output_file);
     }
     $output = $ret = null;
     exec("tidy -config " . escapeshellarg(dirname(__FILE__) . "/tidy.config") . " -q " . ($this->show_body_only ? " --show-body-only yes " : " ") . (" --error-file " . escapeshellarg(dirname(__FILE__)) . "/error.log ") . escapeshellarg($filename), $output, $ret);
     if ($ret == 0) {
         io::done('Tidy-ize done. ');
     } elseif ($ret == 1) {
         io::out('Tidy-ize done, but with warnings. (' . dirname(__FILE__) . "/error.log) ", IO::MESSAGE_WARN);
     } else {
         return io::out("Tidy-ize failed. ", IO::MESSAGE_FAIL) | 3;
     }
     if ($this->tidy_only) {
         if (!empty($this->output_file)) {
             io::out('Writing html to file ');
             $_r = file_put_contents($this->output_file, implode("\n", $output));
             if ($_r === false) {
                 return io::out("Can't write to file. May be permission denied? ", IO::MESSAGE_FAIL) | 5;
             }
             io::done();
         } else {
             echo implode("\n", $output) . "\n";
         }
         return 0;
     }
     $doc = new DOMDocument('1.0', 'utf-8');
     $doc->loadHTML(implode("\n", $output));
     $doc->encoding = "utf-8";
     $subst = array("a" => "WHyperLink", "td" => "WTableColumn", "tr" => "WTableRow", "th" => "WTableHeader", "table" => "WTable", "br" => "WText:br:1", "img" => "WImage", "abbr" => "WText:abbr:1", "acronym" => "WText:acronym:1", "address" => "WText:address:1", "b" => "WText:b:1", "big" => "WText:big:1", "blockquote" => "WText:blockquote:1", "button" => "WButton:type:button", "cite" => "WText:cite:1", "code" => "WText:code:1", "div" => "WBlock", "dfn" => "WText:dfn:1", "em" => "WText:em:1", "fieldset" => "WFieldSet", "form" => "WForm", "h1" => "WText:h:1", "h2" => "WText:h:2", "h3" => "WText:h:3", "h4" => "WText:h:4", "h5" => "WText:h:5", "h6" => "WText:h:6", "hr" => "WText:hr:1", "i" => "WText:i:1", "input" => "WEdit", "ins" => "WText:ins:1", "kbd" => "WText:kbd:1", "li" => "WListItem", "ol" => "WList:ol:1", "option" => "WSelectOption", "p" => "WText:p:1", "pre" => "WText:pre:1", "q" => "WText:q:1", "samp" => "WText:samp:1", "script" => "WInlineScript", "select" => "WSelect", "small" => "WText:small:1", "span" => "WText", "strike" => "WText:strike:1", "strong" => "WText:strong:1", "style" => "WCSS", "sub" => "WText:sub:1", "sup" => "WText:sup:1", "textarea" => "WTextarea", "ul" => "WList", "var" => "WText:var:1", "body" => "root");
     foreach ($subst as $replace_from => $replace_to) {
         @(list($replace_to, $new_attr_name, $new_attr_value) = explode(":", $replace_to));
         $nl = $doc->getElementsByTagName($replace_from);
         for ($i = 0, $c = $nl->length; $i < $c; $i++) {
             $n_dn = $doc->createElement($replace_to);
             $cn = $nl->item(0);
             $cnl = $cn->childNodes;
             if ($cn->hasAttributes()) {
                 foreach ($cn->attributes as $attrName => $attrNode) {
                     if (substr((string) $attrNode->value, 0, 2) != "__" && !empty($attrNode->value)) {
                         $n_dn->setAttribute((string) $attrName, $attrNode->value);
                     }
                 }
             }
             if (isset($new_attr_value, $new_attr_name)) {
                 $n_dn->setAttribute($new_attr_name, $new_attr_value);
             }
             for ($j = 0; $j < $cnl->length; $j++) {
                 if ($cnl->item($j) instanceof DOMText) {
                     $n_dn->appendChild($doc->createTextNode($cnl->item($j)->nodeValue));
                 } else {
                     $n_dn->appendChild($cnl->item($j)->cloneNode(true));
                 }
             }
             $cn->parentNode->replaceChild($n_dn, $cn);
         }
     }
     io::out('Dumping XML...', false);
     if ($this->show_body_only) {
         if (!empty($this->output_file)) {
             file_put_contents($this->output_file, utf8_decode($doc->saveXML($doc->getElementsByTagName("root")->item(0))));
         } else {
             echo utf8_decode($doc->saveXML($doc->getElementsByTagName("root")->item(0)));
         }
     } else {
         if (!empty($this->output_file)) {
             $doc->save($this->output_file);
         } else {
             echo $doc->saveXML();
         }
     }
     io::done();
 }