Posted By

dereklio on 06/10/08


Tagged


Versions (?)

A shell access wrapper for windows in PHP


 / Published in: PHP
 

  1. <?php
  2.  
  3. class Shell {
  4.  
  5. private static $singleton;
  6.  
  7. public static function singleton() {
  8. if (!isset(self::$singleton)) {
  9. $c = __CLASS__;
  10. self::$singleton = new $c;
  11. }
  12.  
  13. return self::$singleton;
  14. }
  15.  
  16. private $dir_temp;
  17.  
  18. private function __construct() {
  19. $this->dir_temp = $this->touch_dir(dirname(__FILE__) . '/temp/');
  20. }
  21.  
  22. public function __clone() {
  23. trigger_error('Clone is not allowed.', E_USER_ERROR);
  24. }
  25.  
  26. public function temp($file = '') {
  27. return $this->dir_temp . $file;
  28. }
  29.  
  30. public function out($text = '') {
  31. if (is_string($text)) {
  32. echo iconv('utf-8', 'big5', $text) . "\n";
  33. } else if (is_bool($text)) {
  34. echo ($text ? 'true' : 'false') . "\n";
  35. } else {
  36. $this->dump($text);
  37. }
  38. }
  39.  
  40. public function dump($obj = false) {
  41. $this->out(print_r($obj, true));
  42. }
  43.  
  44. public function nircmd($cmd) {
  45. $this->out("[nircmd] $cmd");
  46. exec("nircmd $cmd");
  47. }
  48.  
  49. public function concmd_utf8($data, $src_encoding) {
  50. $path = $this->temp('concmd-' . rand(1000, 9999) . '.tmp');
  51. file_put_contents($path, $data);
  52. $cmd = "concmd /i:$src_encoding /o:UTF8 /f:T \"$path\"";
  53. $this->out("[concmd] $cmd");
  54. exec($cmd);
  55. $data = file_get_contents($path);
  56. unlink($path);
  57. return $data;
  58. }
  59.  
  60. public function touch_file($path) {
  61. $path = str_replace('\\', '/', $path);
  62. touch($path);
  63. return $path;
  64. }
  65.  
  66. public function touch_dir($path) {
  67. $path = str_replace('\\', '/', $path);
  68. if (!file_exists($path)) mkdir($path);
  69. return $path;
  70. }
  71. }
  72.  
  73. ?>

Report this snippet  

You need to login to post a comment.