Cette application n'est plus maintenue. Elle reste totalement disponible mais nous vous invitons à utiliser le service GIST qui peut être auto-hébergé.

  1. #!/usr/bin/php
  2. <?php
  3.  
  4. class WallException extends Exception {}
  5.  
  6. class Wall {
  7. private $curl = null;
  8. private $language = 'html';
  9. private $code = null;
  10. private $title = null;
  11. private $showLines = true;
  12.  
  13. private static $instance = null;
  14.  
  15. public static function getInstance() {
  16. return (self::$instance === null) ? self::$instance = new Wall() : self::$instance;
  17. }
  18.  
  19. public function __construct() {
  20. $this->curl = curl_init();
  21. }
  22.  
  23. public function setCode($code) {
  24. $this->code = $code;
  25. return $this;
  26. }
  27.  
  28. public function setCodeFromFile($file) {
  29. $this->code = (file_exists($file) && is_readable($file)) ? file_get_contents($file) : '';
  30. return $this;
  31. }
  32.  
  33. public function getCode() {
  34. return $this->code;
  35. }
  36.  
  37. public function setLanguage($language) {
  38. $this->language = $language;
  39. return $this;
  40. }
  41.  
  42. public function getLanguage() {
  43. return $this->language;
  44. }
  45.  
  46. public function setTitle($title) {
  47. $this->title = $title;
  48. return $this;
  49. }
  50.  
  51. public function getTitle() {
  52. return $this->title;
  53. }
  54.  
  55. public function setShowLines($showLines) {
  56. $this->showLines = (bool)$showLines;
  57. return $this;
  58. }
  59.  
  60. public function getShowLines() {
  61. return $this->showLines;
  62. }
  63.  
  64. public function paste() {
  65. $options = array(
  66. CURLOPT_URL => 'http://wall.deblan.fr/',
  67. CURLOPT_HEADER => true,
  68. CURLOPT_FOLLOWLOCATION => true,
  69. CURLOPT_RETURNTRANSFER => true,
  70. CURLOPT_POST => true,
  71. CURLOPT_POSTFIELDS => http_build_query(array(
  72. 'code' => $this->getCode(),
  73. 'language' => $this->getLanguage(),
  74. 'title' => $this->getTitle(),
  75. 'line' => $this->getShowLines()
  76. ))
  77. );
  78.  
  79. curl_setopt_array($this->curl, $options);
  80.  
  81. $header = curl_exec($this->curl);
  82.  
  83. if(curl_errno($this->curl)) {
  84. throw new WallException('Curl error: '.curl_error($this->curl));
  85. }
  86. else {
  87. $http_code = curl_getinfo($this->curl, CURLINFO_HTTP_CODE);
  88. if(in_array($http_code, array(301, 302, 200))) {
  89. preg_match('`location: ([^\s]+)`i', $header, $matches);
  90. return 'http://wall.deblan.fr'.trim(array_pop($matches));
  91. } else {
  92. throw new WallException('HTTP header error: unexpected response code ('.$http_code.')');
  93. }
  94. }
  95. }
  96. }
  97.  
  98. function getHelp() {
  99. global $argv;
  100. return $argv[0].' <file> [language]'.PHP_EOL.'Using stdin: '.$argv[0].' - [language]'.PHP_EOL;
  101. }
  102.  
  103.  
  104. try {
  105. if(!isset($argv[1])) {
  106. throw new InvalidArgumentException('I miss the first parameter...');
  107. }
  108.  
  109. Wall::getInstance()->setLanguage(isset($argv[2]) ? $argv[2] : 'texte');
  110.  
  111. if($argv[1] == '-') {
  112. $handle = fopen('php://stdin', 'r');
  113. $code = "";
  114.  
  115. while(!feof($handle)) {
  116. $code.= rtrim(fgets($handle)).PHP_EOL;
  117. }
  118.  
  119. Wall::getInstance()->setCode($code);
  120. }
  121. else {
  122. Wall::getInstance()->setCodeFromFile($argv[1]);
  123. }
  124.  
  125.  
  126. echo Wall::getInstance()->paste(), PHP_EOL;
  127. }
  128. catch(WallException $e) {
  129. echo 'WALL ERROR: '.$e->getMessage();
  130. echo getHelp();
  131. }
  132. catch(Exception $e) {
  133. echo 'SCRIPT ERROR: ', $e->getMessage(), PHP_EOL;
  134. echo getHelp();
  135. }
  136.