symball /
report
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 | |||
| 3 | /* |
||
| 4 | * This file is part of the ReportBundle package |
||
| 5 | * |
||
| 6 | * (c) symball <http://simonball.me> |
||
| 7 | * |
||
| 8 | * For the full copyright and license information, please view the LICENSE file |
||
| 9 | * that was distributed with this source code. |
||
| 10 | */ |
||
| 11 | |||
| 12 | namespace Symball\ReportBundle\Service; |
||
| 13 | |||
| 14 | use Symfony\Component\Filesystem\Filesystem; |
||
| 15 | use Symfony\Component\HttpFoundation\File\File; |
||
| 16 | |||
| 17 | /** |
||
| 18 | * The Excel service handles the start and end of spreadsheet creation |
||
| 19 | * |
||
| 20 | * @author Simon Ball <simonball at simonball dot me> |
||
| 21 | */ |
||
| 22 | class Excel |
||
| 23 | { |
||
| 24 | /* @var $excelFactoryNameSpace Namespace PHPExcel Factory IO */ |
||
| 25 | private $excelFactoryNameSpace; |
||
| 26 | |||
| 27 | /* @var $excelObject PHPExcel */ |
||
| 28 | private $excelObject; |
||
| 29 | |||
| 30 | |||
| 31 | private $numberOfSheets = 0; |
||
| 32 | protected $currentSheetTitle; |
||
| 33 | |||
| 34 | private $sheet; |
||
| 35 | |||
| 36 | private $reportPath; |
||
| 37 | |||
| 38 | private $outputFormat; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * |
||
| 42 | * @param string $defaultReportPath |
||
| 43 | * @param string $excelFactoryNameSpace |
||
| 44 | * @param string $outputFormat |
||
| 45 | */ |
||
| 46 | public function __construct( |
||
| 47 | $defaultReportPath = '', |
||
| 48 | $excelFactoryNameSpace = '\PHPExcel_IOFactory', |
||
| 49 | $outputFormat = 'Excel2007' |
||
| 50 | ) { |
||
| 51 | if ($defaultReportPath) { |
||
| 52 | $this->setReportPath($defaultReportPath); |
||
| 53 | } |
||
| 54 | $this->excelFactoryNameSpace = $excelFactoryNameSpace; |
||
| 55 | $this->setOutputFormat($outputFormat); |
||
| 56 | } |
||
| 57 | |||
| 58 | /** |
||
| 59 | * Initiate a PHP Excel object |
||
| 60 | * |
||
| 61 | * @return $this |
||
| 62 | */ |
||
| 63 | public function createExcelObject() |
||
| 64 | { |
||
| 65 | $this->excelObject = new \PHPExcel(); |
||
| 66 | |||
| 67 | return $this; |
||
| 68 | } |
||
| 69 | |||
| 70 | /** |
||
| 71 | * Create a new sheet within the excel object and ready the service for it. |
||
| 72 | * This in essence resets most things so make sure you have finished working |
||
| 73 | * on your sheet before creating another. |
||
| 74 | * |
||
| 75 | * @param string $title The name of the sheet |
||
| 76 | * |
||
| 77 | * @return [type] [description] |
||
|
0 ignored issues
–
show
|
|||
| 78 | */ |
||
| 79 | public function newSheet($title = '') |
||
| 80 | { |
||
| 81 | if (!$this->excelObject) { |
||
| 82 | $this->createExcelObject(); |
||
| 83 | } |
||
| 84 | ++$this->numberOfSheets; |
||
| 85 | |||
| 86 | // If there is already the default initiated sheet, create new sheet |
||
| 87 | if ($this->numberOfSheets > 0) { |
||
| 88 | $this->excelObject->createSheet($this->numberOfSheets); |
||
| 89 | } |
||
| 90 | |||
| 91 | // Has a title been set |
||
| 92 | if (!$title) { |
||
| 93 | $title = 'sheet-' . $this->numberOfSheets; |
||
| 94 | } |
||
| 95 | $this->setCurrentSheetTitle($title); |
||
| 96 | |||
| 97 | $this->sheet = $this->excelObject->setActiveSheetIndex($this->numberOfSheets); |
||
| 98 | $this->sheet->setTitle($title); |
||
| 99 | |||
| 100 | return $this->sheet; |
||
| 101 | } |
||
| 102 | |||
| 103 | /** |
||
| 104 | * Use the PHPExcel factory writer and output the current excel object in to |
||
| 105 | * a file |
||
| 106 | * |
||
| 107 | * @param string $fileName |
||
| 108 | * @param string $path |
||
| 109 | * @param string $outputFormat |
||
| 110 | * @return File |
||
| 111 | * @throws \Exception When trying to use on the fly unsupported output format |
||
| 112 | */ |
||
| 113 | public function save($fileName, $path = '', $outputFormat = '') |
||
| 114 | { |
||
| 115 | |||
| 116 | // If the user is trying to specify file format themself, check it is |
||
| 117 | // usable |
||
| 118 | if ($outputFormat) { |
||
| 119 | if ($this->checkOutputFormat($outputFormat) === false) { |
||
| 120 | throw new \Exception('Output format not supported: ' . $outputFormat); |
||
| 121 | } |
||
| 122 | } else { |
||
| 123 | $outputFormat = $this->getOutputFormat(); |
||
| 124 | } |
||
| 125 | |||
| 126 | $writer = call_user_func( |
||
| 127 | $this->excelFactoryNameSpace . '::createWriter', |
||
| 128 | $this->excelObject, |
||
| 129 | $outputFormat |
||
| 130 | ); |
||
| 131 | |||
| 132 | if (!$path) { |
||
| 133 | $path = $this->reportPath; |
||
| 134 | } |
||
| 135 | |||
| 136 | $filePath = $path . '/' . $fileName; |
||
| 137 | |||
| 138 | $writer->save($filePath); |
||
| 139 | $file = new File($filePath); |
||
| 140 | |||
| 141 | return $file; |
||
| 142 | } |
||
| 143 | |||
| 144 | /** |
||
| 145 | * Set a file path where the saved report will be output to. |
||
| 146 | * |
||
| 147 | * @param string $path |
||
| 148 | * @param boolean $createPath |
||
| 149 | * @throws \Exception |
||
| 150 | */ |
||
| 151 | public function setReportPath($path, $createPath = true) |
||
| 152 | { |
||
| 153 | // TODO Remove reliance on concrete Symfony file class and use interface |
||
| 154 | $fileSystem = new Filesystem(); |
||
| 155 | if (!$fileSystem->exists($path)) { |
||
| 156 | if ($createPath) { |
||
| 157 | $fileSystem->mkdir($path); |
||
| 158 | } else { |
||
| 159 | throw new \Exception('Report path does not exist: ' . $path); |
||
| 160 | } |
||
| 161 | } |
||
| 162 | $this->reportPath = $path; |
||
| 163 | } |
||
| 164 | |||
| 165 | /** |
||
| 166 | * Set the "excel type" that will PHPExcel will output |
||
| 167 | * Excel2007 / Excel5 / Excel2003XML / SYLK / OOCalc / CSV / HTML. |
||
| 168 | * |
||
| 169 | * @param string $format [description] |
||
| 170 | */ |
||
| 171 | public function setOutputFormat($format) |
||
| 172 | { |
||
| 173 | |||
| 174 | // TODO - After converting to facade, add check supported type function |
||
| 175 | // Check that it is one of the accepted types |
||
| 176 | if ($this->checkOutputFormat($format) === false) { |
||
| 177 | throw new \Exception('Output format not supported: ' . $format); |
||
| 178 | } |
||
| 179 | |||
| 180 | $this->outputFormat = $format; |
||
| 181 | } |
||
| 182 | |||
| 183 | /** |
||
| 184 | * @return string |
||
| 185 | */ |
||
| 186 | public function getCurrentSheetTitle() |
||
| 187 | { |
||
| 188 | return $this->currentSheetTitle; |
||
| 189 | } |
||
| 190 | |||
| 191 | /** |
||
| 192 | * @return integer |
||
| 193 | */ |
||
| 194 | public function getNumberOfSheets() |
||
| 195 | { |
||
| 196 | return $this->numberOfSheets; |
||
| 197 | } |
||
| 198 | |||
| 199 | /** |
||
| 200 | * @return \PHPExcel |
||
| 201 | */ |
||
| 202 | public function getExcelObject() |
||
| 203 | { |
||
| 204 | return $this->excelObject; |
||
| 205 | } |
||
| 206 | |||
| 207 | /** |
||
| 208 | * @return string |
||
| 209 | */ |
||
| 210 | public function getReportPath() |
||
| 211 | { |
||
| 212 | return $this->reportPath; |
||
| 213 | } |
||
| 214 | |||
| 215 | /** |
||
| 216 | * @return string |
||
| 217 | */ |
||
| 218 | public function getOutputFormat() |
||
| 219 | { |
||
| 220 | return $this->outputFormat; |
||
| 221 | } |
||
| 222 | |||
| 223 | /** |
||
| 224 | * Take a given format and check whether it is compatible with what PHPExcel |
||
| 225 | * is able to export |
||
| 226 | * |
||
| 227 | * @param string $format |
||
| 228 | * @return boolean|null |
||
| 229 | */ |
||
| 230 | public function checkOutputFormat($format) |
||
| 231 | { |
||
| 232 | $supportedTypes = [ |
||
| 233 | 'Excel2007', |
||
| 234 | 'Excel5', |
||
| 235 | 'Excel2003XML', |
||
| 236 | 'SYLK', |
||
| 237 | 'OOCalc', |
||
| 238 | 'CSV', |
||
| 239 | 'HTML', |
||
| 240 | ]; |
||
| 241 | |||
| 242 | if (in_array($format, $supportedTypes)) { |
||
| 243 | return true; |
||
| 244 | } |
||
| 245 | } |
||
| 246 | |||
| 247 | /** |
||
| 248 | * @param string $title |
||
| 249 | */ |
||
| 250 | protected function setCurrentSheetTitle($title) |
||
| 251 | { |
||
| 252 | $this->currentSheetTitle = $title; |
||
| 253 | } |
||
| 254 | } |
||
| 255 |
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.