fwolf /
fwlib
This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include, or for example
via PHP's auto-loading mechanism.
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
| 1 | <?php |
||
| 2 | namespace Fwlib\Html\TextDocument; |
||
| 3 | |||
| 4 | use Fwlib\Html\TextDocument\AbstractTextConverter; |
||
| 5 | |||
| 6 | /** |
||
| 7 | * Text converter for reStructuredText |
||
| 8 | * |
||
| 9 | * @codeCoverageIgnore |
||
| 10 | * |
||
| 11 | * @copyright Copyright 2013-2015 Fwolf |
||
| 12 | * @license http://www.gnu.org/licenses/lgpl.html LGPL-3.0+ |
||
| 13 | */ |
||
| 14 | class Restructuredtext extends AbstractTextConverter |
||
| 15 | { |
||
| 16 | /** |
||
| 17 | * Cmd options when used rst2xxx.py |
||
| 18 | * |
||
| 19 | * Param is combined in, eg: tab-width=4 |
||
| 20 | * |
||
| 21 | * @var array |
||
| 22 | */ |
||
| 23 | public $cmdOption = [ |
||
| 24 | 'embed-stylesheet', |
||
| 25 | //'link-stylesheet', |
||
| 26 | |||
| 27 | // h1 is for title, document section/title start from h2 |
||
| 28 | 'initial-header-level=2', |
||
| 29 | |||
| 30 | //'no-doc-title', |
||
| 31 | //'no-xml-declaration', |
||
| 32 | 'cloak-email-addresses', |
||
| 33 | ]; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * Actual path of docutils execute file |
||
| 37 | * |
||
| 38 | * @var string |
||
| 39 | * @see setPathDocutils() |
||
| 40 | */ |
||
| 41 | protected $pathDocutils = ''; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * Use pipe to execute cmd, not use temporary file |
||
| 45 | * |
||
| 46 | * @var boolean |
||
| 47 | */ |
||
| 48 | public $usePipe = true; |
||
| 49 | |||
| 50 | |||
| 51 | /** |
||
| 52 | * Constructor |
||
| 53 | * |
||
| 54 | * @var param string $pathDocutils |
||
| 55 | */ |
||
| 56 | public function __construct($pathDocutils = '') |
||
| 57 | { |
||
| 58 | // Convert html only, leave CSS link from outside |
||
| 59 | $this->setPathDocutils($pathDocutils); |
||
| 60 | } |
||
| 61 | |||
| 62 | |||
| 63 | /** |
||
| 64 | * Convert string to html |
||
| 65 | * |
||
| 66 | * I had run benchmark to compare pipe and tmp file before migrate to PSR |
||
| 67 | * standard, result is, their speed are almost same. |
||
| 68 | * |
||
| 69 | * @param string $str |
||
| 70 | * @param boolean $bodyOnly |
||
| 71 | * @return string |
||
| 72 | */ |
||
| 73 | public function convertString($str, $bodyOnly = true) |
||
| 74 | { |
||
| 75 | if (!is_executable($this->pathDocutils)) { |
||
| 76 | throw new \Exception('Cannot found docutils executable.'); |
||
| 77 | } |
||
| 78 | |||
| 79 | |||
| 80 | $cmd = $this->pathDocutils . $this->genCmdOptionString(); |
||
| 81 | $cmd = escapeshellcmd($cmd); |
||
| 82 | |||
| 83 | if ($this->usePipe) { |
||
| 84 | $desc = [ |
||
| 85 | 0 => ['pipe', 'r'], |
||
| 86 | 1 => ['pipe', 'w'], |
||
| 87 | ]; |
||
| 88 | |||
| 89 | $proc = proc_open($cmd, $desc, $pipes); |
||
| 90 | if (!is_resource($proc)) { |
||
| 91 | throw new \Exception('Open pipe fail.'); |
||
| 92 | } |
||
| 93 | |||
| 94 | $fp = $pipes[0]; |
||
| 95 | fwrite($fp, $str); |
||
| 96 | fflush($fp); |
||
| 97 | fclose($fp); |
||
| 98 | |||
| 99 | $html = ''; |
||
| 100 | while (!feof($pipes[1])) { |
||
| 101 | $html .= fgets($pipes[1]); |
||
| 102 | } |
||
| 103 | fclose($pipes[1]); |
||
| 104 | |||
| 105 | proc_close($proc); |
||
| 106 | |||
| 107 | View Code Duplication | } else { |
|
|
0 ignored issues
–
show
|
|||
| 108 | // Use tmp file |
||
| 109 | $tmpFile = tempnam(sys_get_temp_dir(), 'fwlib-html-restructuredtext-'); |
||
| 110 | file_put_contents($tmpFile, $str); |
||
| 111 | |||
| 112 | // Execute cmd, got result |
||
| 113 | $cmd .= " $tmpFile"; |
||
| 114 | exec($cmd, $output); |
||
| 115 | |||
| 116 | unlink($tmpFile); |
||
| 117 | $html = implode("\n", $output); |
||
| 118 | } |
||
| 119 | |||
| 120 | |||
| 121 | if ($bodyOnly) { |
||
| 122 | // Trim html, only leave content in <body> |
||
| 123 | $i = strpos($html, '<body>'); |
||
| 124 | if (!(false === $i)) { |
||
| 125 | $html = substr($html, $i + 6); |
||
| 126 | } |
||
| 127 | $i = strrpos($html, '</body>'); |
||
| 128 | if (!(false === $i)) { |
||
| 129 | $html = substr($html, 0, $i); |
||
| 130 | } |
||
| 131 | } |
||
| 132 | |||
| 133 | |||
| 134 | return $html; |
||
| 135 | } |
||
| 136 | |||
| 137 | |||
| 138 | /** |
||
| 139 | * Get title of text content if possible |
||
| 140 | * |
||
| 141 | * @param string $source String or filename to convert |
||
| 142 | * @return string |
||
| 143 | */ |
||
| 144 | View Code Duplication | public function getTitle($source) |
|
|
0 ignored issues
–
show
This method seems to be duplicated in your project.
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation. You can also find more detailed suggestions in the “Code” section of your repository. Loading history...
|
|||
| 145 | { |
||
| 146 | if ($this->isFile($source)) { |
||
| 147 | $source = file_get_contents($source); |
||
| 148 | } |
||
| 149 | |||
| 150 | // Title adornment character |
||
| 151 | $tac = '(=+|-+|\\`+|\\:+|\\.+|\\\'+|\\"+|\\~+|\\^+|_+|\\*+|\\++|\\#+)'; |
||
| 152 | |||
| 153 | $i = preg_match("/$tac\\s*\\n(.+?)\\n\\1\\s*\\n/mx", $source, $ar); |
||
| 154 | |||
| 155 | if (0 == $i) { |
||
| 156 | // Not found |
||
| 157 | return null; |
||
| 158 | } else { |
||
| 159 | return $ar[2]; |
||
| 160 | } |
||
| 161 | } |
||
| 162 | |||
| 163 | |||
| 164 | |||
| 165 | /** |
||
| 166 | * Gen string include cmd option for exec |
||
| 167 | * |
||
| 168 | * @return string |
||
| 169 | */ |
||
| 170 | View Code Duplication | protected function genCmdOptionString() |
|
|
0 ignored issues
–
show
This method seems to be duplicated in your project.
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation. You can also find more detailed suggestions in the “Code” section of your repository. Loading history...
|
|||
| 171 | { |
||
| 172 | if (empty($this->cmdOption)) { |
||
| 173 | return ''; |
||
| 174 | } else { |
||
| 175 | $s = ' '; |
||
| 176 | foreach ($this->cmdOption as $v) { |
||
| 177 | // Single char param without '-' |
||
| 178 | if (1 == strlen($v)) { |
||
| 179 | $v = '-' . $v; |
||
| 180 | } elseif (1 < strlen($v) && '--' != substr($v, 0, 2)) { |
||
| 181 | // Multi char param without '--' |
||
| 182 | $v = '--' . $v; |
||
| 183 | } |
||
| 184 | |||
| 185 | $s .= $v . ' '; |
||
| 186 | } |
||
| 187 | |||
| 188 | return $s; |
||
| 189 | } |
||
| 190 | } |
||
| 191 | |||
| 192 | |||
| 193 | /** |
||
| 194 | * Set path of docutils or detect it |
||
| 195 | * |
||
| 196 | * @param $path Manual additional path |
||
| 197 | * @return string |
||
| 198 | */ |
||
| 199 | public function setPathDocutils($path = '') |
||
| 200 | { |
||
| 201 | // Possible path of docutils execute file for choose |
||
| 202 | $possiblePath = [ |
||
| 203 | '/usr/bin/', |
||
| 204 | '/usr/local/bin/', |
||
| 205 | '/bin/', |
||
| 206 | ]; |
||
| 207 | |||
| 208 | if (!empty($path)) { |
||
| 209 | // Prepend to array |
||
| 210 | array_unshift($possiblePath, $path); |
||
| 211 | } |
||
| 212 | |||
| 213 | // Find a usable path |
||
| 214 | $found = false; |
||
| 215 | while (!$found && !empty($possiblePath)) { |
||
| 216 | $path = array_shift($possiblePath); |
||
| 217 | |||
| 218 | if (is_executable($path . 'rst2html.py')) { |
||
| 219 | $found = true; |
||
|
0 ignored issues
–
show
$found is not used, you could remove the assignment.
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently. $myVar = 'Value';
$higher = false;
if (rand(1, 6) > 3) {
$higher = true;
} else {
$higher = false;
}
Both the Loading history...
|
|||
| 220 | $this->pathDocutils = $path . 'rst2html.py'; |
||
| 221 | break; |
||
| 222 | } |
||
| 223 | |||
| 224 | // In some env like my (MT) CentOS 5, cmd hasn't .py extension |
||
| 225 | if (is_executable($path . 'rst2html')) { |
||
| 226 | $found = true; |
||
|
0 ignored issues
–
show
$found is not used, you could remove the assignment.
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently. $myVar = 'Value';
$higher = false;
if (rand(1, 6) > 3) {
$higher = true;
} else {
$higher = false;
}
Both the Loading history...
|
|||
| 227 | $this->pathDocutils = $path . 'rst2html'; |
||
| 228 | break; |
||
| 229 | } |
||
| 230 | } |
||
| 231 | |||
| 232 | return $this->pathDocutils; |
||
| 233 | } |
||
| 234 | } |
||
| 235 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.