EVE-KILL /
zKillboard
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
| 1 | #!/usr/bin/env php |
||
|
0 ignored issues
–
show
|
|||
| 2 | <?php |
||
| 3 | /* zKillboard |
||
| 4 | * Copyright (C) 2012-2015 EVE-KILL Team and EVSCO. |
||
| 5 | * |
||
| 6 | * This program is free software: you can redistribute it and/or modify |
||
| 7 | * it under the terms of the GNU Affero General Public License as published by |
||
| 8 | * the Free Software Foundation, either version 3 of the License, or |
||
| 9 | * (at your option) any later version. |
||
| 10 | * |
||
| 11 | * This program is distributed in the hope that it will be useful, |
||
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
||
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||
| 14 | * GNU Affero General Public License for more details. |
||
| 15 | * |
||
| 16 | * You should have received a copy of the GNU Affero General Public License |
||
| 17 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
||
| 18 | */ |
||
| 19 | |||
| 20 | $cle = "cli" == php_sapi_name(); |
||
| 21 | if (!$cle) return; // Prevent web execution |
||
| 22 | |||
| 23 | if(getenv("SILENT_CLI")) |
||
| 24 | { |
||
| 25 | ob_start("obCallback"); |
||
| 26 | ob_implicit_flush(); |
||
| 27 | } |
||
| 28 | |||
| 29 | $base = dirname(__FILE__); |
||
| 30 | |||
| 31 | require_once( "config.php" ); |
||
| 32 | |||
| 33 | require_once( "init.php" ); |
||
| 34 | |||
| 35 | array_shift($argv); |
||
| 36 | $command = array_shift($argv); |
||
| 37 | |||
| 38 | if($command == "bashList") |
||
| 39 | listCommands(); |
||
| 40 | |||
| 41 | try |
||
| 42 | { |
||
| 43 | $fileName = "$base/cli/cli_$command.php"; |
||
| 44 | if ($command == "") |
||
| 45 | CLI::out("|r|You haven't issued a command to execute.|n| Please use list to show all commands, or help <command> to see information on how to use the command", true); |
||
| 46 | if(!file_exists($fileName)) |
||
| 47 | CLI::out("|r|Error running $command|n|. Please use list to show all commands, or help <command> to see information on how to use the command", true); |
||
| 48 | |||
| 49 | require_once $fileName; |
||
| 50 | $className = "cli_$command"; |
||
| 51 | $class = new $className(); |
||
| 52 | |||
| 53 | if(!is_a($class, "cliCommand")) |
||
| 54 | CLI::out("|r| Module $command does not implement interface cliCommand", true); |
||
| 55 | |||
| 56 | $db = new Db(); |
||
| 57 | $db->execute("set session wait_timeout = 600"); |
||
| 58 | $class->execute($argv, $db); |
||
| 59 | } |
||
| 60 | catch (Exception $ex) |
||
| 61 | { |
||
| 62 | CLI::out("$command ended with error: " . $ex->getMessage(), true); |
||
| 63 | } |
||
| 64 | |||
| 65 | interface cliCommand { |
||
| 66 | |||
| 67 | /** |
||
| 68 | * @return string |
||
| 69 | */ |
||
| 70 | public function getDescription(); |
||
| 71 | |||
| 72 | /** |
||
| 73 | * @return string |
||
| 74 | */ |
||
| 75 | public function getAvailMethods(); |
||
| 76 | |||
| 77 | /** |
||
| 78 | * @return void |
||
| 79 | */ |
||
| 80 | public function execute($parameters, $db); |
||
| 81 | } |
||
| 82 | |||
| 83 | function listCommands() |
||
| 84 | { |
||
| 85 | $commands = array(); |
||
| 86 | $dir = __DIR__."/cli/"; |
||
| 87 | |||
| 88 | if($handle = opendir($dir)) |
||
| 89 | { |
||
| 90 | while(false !== ($entry = readdir($handle))) |
||
| 91 | { |
||
| 92 | if($entry != "." && $entry != ".." && $entry != "base.php" && $entry != "cli_methods.php") |
||
| 93 | { |
||
| 94 | $s1 = explode("cli_", $entry); |
||
| 95 | $s2 = explode(".php", @$s1[1]); |
||
| 96 | if(sizeof($s2) == 2) |
||
| 97 | { |
||
| 98 | require_once "$dir/$entry"; |
||
| 99 | $command = $s2[0]; |
||
| 100 | $className = "cli_$command"; |
||
| 101 | $class = new $className(); |
||
| 102 | if(is_a($class, "cliCommand")) |
||
| 103 | { |
||
| 104 | $commands[] = $command; |
||
| 105 | } |
||
| 106 | } |
||
| 107 | } |
||
| 108 | } |
||
| 109 | closedir($handle); |
||
| 110 | } |
||
| 111 | sort($commands); |
||
| 112 | CLI::out(implode(" ", $commands), true); |
||
| 113 | } |
||
| 114 | |||
| 115 | function obCallback($buf) |
||
| 116 | { |
||
| 117 | // Maybe log it somewhere, but for now just throw it away. |
||
| 118 | return ""; |
||
| 119 | } |
||
| 120 |
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.