/rhaco/io/Stream.php

https://github.com/tokushima/rhaco1 · PHP · 151 lines · 99 code · 5 blank · 47 comment · 22 complexity · 5a3bc00a8e1ef1dc90a2e6811215d135 MD5 · raw file

  1. <?php
  2. Rhaco::import("network.http.Http");
  3. Rhaco::import("io.FileUtil");
  4. Rhaco::import("lang.StringUtil");
  5. Rhaco::import("exception.ExceptionTrigger");
  6. Rhaco::import("exception.model.PermissionException");
  7. /**
  8. * ストリームを操作するクラス
  9. *
  10. * @author Kazutaka Tokushima
  11. * @license New BSD License
  12. * @copyright Copyright 2005- rhaco project. All rights reserved.
  13. */
  14. class Stream{
  15. /**
  16. * ストリームから取得する
  17. * @param string $url
  18. * @return string
  19. */
  20. function read($url){
  21. /*** #pass */
  22. $src = "";
  23. if(preg_match("/[\w]+:\/\/[\w]+/",$url)){
  24. if(preg_match("/php:\/\/(.+)/i",$url,$type)){
  25. $type = strtolower($type);
  26. if($type == "stdin"){
  27. $src = Stream::stdin();
  28. }else if($type == "input"){
  29. $src = Stream::input();
  30. }
  31. }else{
  32. $src = Http::get($url);
  33. }
  34. }else{
  35. $src = FileUtil::read($url);
  36. }
  37. return StringUtil::encode($src);
  38. }
  39. /**
  40. * ストリームに書き出す
  41. * @param string $url
  42. * @param string $value
  43. */
  44. function write($url,$value){
  45. /*** #pass */
  46. if(preg_match("/[\w]+:\/\/[\w]+/",$url)){
  47. if(preg_macth("/php:\/\/(.+)/i",$url,$type)){
  48. $type = strtolower($type);
  49. if($type == "stdout"){
  50. Stream::stdout($value);
  51. }else if($type == "stderr"){
  52. Stream::stderr($value);
  53. }else if($type == "output"){
  54. Stream::output($value);
  55. }
  56. }
  57. }else{
  58. FileUtil::write($url,$value);
  59. }
  60. }
  61. /**
  62. * 標準入力から取得
  63. * @return string
  64. */
  65. function stdin(){
  66. /*** #pass */
  67. $buffer = "";
  68. $fp = fopen("php://stdin","r");
  69. if(!$fp){
  70. ExceptionTrigger::raise(new PermissionException("php://stdin"));
  71. return false;
  72. }
  73. while(substr($buffer,-1) != "\n" && substr($buffer,-1) != "\r\n"){
  74. $buffer .= fgets($fp,4096);
  75. }
  76. fclose($fp);
  77. return rtrim($buffer);
  78. }
  79. /**
  80. * php://input から取得
  81. * @return string
  82. */
  83. function input(){
  84. /*** #pass */
  85. $buffer = "";
  86. $fp = fopen("php://input","r");
  87. if(!$fp){
  88. ExceptionTrigger::raise(new PermissionException("php://input"));
  89. return false;
  90. }
  91. while(!feof($fp)){
  92. $buffer .= fgets($fp,4096);
  93. }
  94. fclose($fp);
  95. return $buffer;
  96. }
  97. /**
  98. * 標準出力へ出力
  99. * @param string $value
  100. * @return boolean
  101. */
  102. function stdout($value){
  103. /*** #pass */
  104. $fp = fopen("php://stdout","w");
  105. if(!$fp){
  106. ExceptionTrigger::raise(new PermissionException("php://stdout"));
  107. return false;
  108. }
  109. fwrite($fp,$value);
  110. fclose($fp);
  111. return true;
  112. }
  113. /**
  114. * 標準エラー出力へ出力
  115. * @param $value
  116. * @return boolean
  117. */
  118. function stderr($value){
  119. /*** #pass */
  120. $fp = fopen("php://stderr","w");
  121. if(!$fp){
  122. ExceptionTrigger::raise(new PermissionException("php://stderr"));
  123. return false;
  124. }
  125. fwrite($fp,$value);
  126. fclose($fp);
  127. return true;
  128. }
  129. /**
  130. * php://output へ出力
  131. * @param string $value
  132. * @return boolean
  133. */
  134. function output($value){
  135. /*** #pass */
  136. $fp = fopen("php://output","w");
  137. if(!$fp){
  138. ExceptionTrigger::raise(new PermissionException("php://output"));
  139. return false;
  140. }
  141. fwrite($fp,$value);
  142. fclose($fp);
  143. return true;
  144. }
  145. }
  146. ?>