|
13 | 13 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
14 | 14 | // See the License for the specific language governing permissions and |
15 | 15 | // limitations under the License. |
| 16 | +require_once("api/framework/APITools.inc"); |
16 | 17 |
|
17 | | -# MAKEENDPOINTS COMMAND |
18 | | -if ($argv[1] === "buildendpoints") { |
| 18 | +function build_endpoints() { |
19 | 19 | # Import each endpoint class |
20 | 20 | foreach(glob("/etc/inc/api/endpoints/*.inc") as $file) { |
21 | 21 | # Import classes files and create object |
|
39 | 39 | if (!is_null($endpoint_obj->url) and is_file("/usr/local/www".$endpoint_obj->url."/index.php")) { |
40 | 40 | echo "Builing ".$endpoint_class." endpoint at URL \"".$endpoint_obj->url."\"... done.".PHP_EOL; |
41 | 41 | } else { |
42 | | - echo "Failed to build ".$endpoint_class." endpoint at URL \"".$endpoint_obj->url."\"".PHP_EOL; |
| 42 | + echo "Builing ".$endpoint_class." endpoint at URL \"".$endpoint_obj->url."\"... failed.".PHP_EOL; |
43 | 43 | exit(1); |
44 | 44 | } |
45 | 45 | } |
46 | 46 | } |
| 47 | + |
| 48 | +function backup() { |
| 49 | + # Local Variables |
| 50 | + $api_conf = APITools\get_api_config()[1]; |
| 51 | + $backup_api_conf = json_encode($api_conf); |
| 52 | + file_put_contents("/usr/local/share/pfSense-pkg-API/backup.json", $backup_api_conf); |
| 53 | + echo "Backing up API configuration... done.".PHP_EOL; |
| 54 | +} |
| 55 | + |
| 56 | +function restore() { |
| 57 | + global $config; |
| 58 | + |
| 59 | + # Local Variables |
| 60 | + $api_conf = APITools\get_api_config(); |
| 61 | + $backup_api_conf_json = file_get_contents("/usr/local/share/pfSense-pkg-API/backup.json"); |
| 62 | + $backup_api_conf = json_decode($backup_api_conf_json, true);; |
| 63 | + |
| 64 | + # Restore our API configuration if the backup exists |
| 65 | + if (!empty($backup_api_conf_json)) { |
| 66 | + # Only restore the config if it has changed |
| 67 | + if ($api_conf[1] !== $backup_api_conf) { |
| 68 | + $config["installedpackages"]["package"][$api_conf[0]]["conf"] = $backup_api_conf; |
| 69 | + write_config("Synchronized persistent API configuration"); |
| 70 | + echo "Restoring API configuration... done.".PHP_EOL; |
| 71 | + } else { |
| 72 | + echo "Restoring API configuration... no changes found.".PHP_EOL; |
| 73 | + } |
| 74 | + } else { |
| 75 | + echo "Restoring API configuration... no configuration found.".PHP_EOL; |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +function update() { |
| 80 | + echo shell_exec("/usr/sbin/pkg delete -y pfSense-pkg-API"); |
| 81 | + echo shell_exec("/usr/sbin/pkg add https://github.com/jaredhendrickson13/pfsense-api/releases/latest/download/pfSense-2.4-pkg-API.txz"); |
| 82 | + echo shell_exec("/etc/rc.restart_webgui"); |
| 83 | +} |
| 84 | + |
| 85 | +function revert($version) { |
| 86 | + # Local variables |
| 87 | + $pf_version = substr(file_get_contents("/etc/version"), 0, 3); |
| 88 | + $url = "https://github.com/jaredhendrickson13/pfsense-api/releases/download/".urlencode($version)."/pfSense-".$pf_version."-pkg-API.txz"; |
| 89 | + echo "Locating pfSense-pkg-API-".$version." at ".$url."... "; |
| 90 | + |
| 91 | + # Check our URL for the existence of this version |
| 92 | + $headers = @get_headers($url); |
| 93 | + |
| 94 | + # If this release exists, remove the existing package and install the requested one. Otherwise return error |
| 95 | + if($headers && strpos($headers[0], '302')) { |
| 96 | + echo "done.".PHP_EOL; |
| 97 | + echo shell_exec("/usr/sbin/pkg delete -y pfSense-pkg-API"); |
| 98 | + echo shell_exec("/usr/sbin/pkg add ".$url); |
| 99 | + echo shell_exec("/etc/rc.restart_webgui"); |
| 100 | + } else { |
| 101 | + echo "no package found.".PHP_EOL; |
| 102 | + exit(1); |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +function delete() { |
| 107 | + echo shell_exec("/usr/sbin/pkg delete -y pfSense-pkg-API"); |
| 108 | + echo shell_exec("/etc/rc.restart_webgui"); |
| 109 | +} |
| 110 | + |
| 111 | +function version() { |
| 112 | + echo shell_exec("/usr/sbin/pkg info pfSense-pkg-API"); |
| 113 | +} |
| 114 | + |
| 115 | +function help() { |
| 116 | + echo "pfsense-api - CLI tool for pfSense API management".PHP_EOL; |
| 117 | + echo "Copyright - ".date("Y")."© - Jared Hendrickson".PHP_EOL; |
| 118 | + echo "SYNTAX:".PHP_EOL; |
| 119 | + echo " pfsense-api <command> <args>".PHP_EOL; |
| 120 | + echo "COMMANDS:".PHP_EOL; |
| 121 | + echo " version : Display the current package version and build information".PHP_EOL; |
| 122 | + echo " help : Display the help page (this page)".PHP_EOL; |
| 123 | + echo " buildendpoints : Build all API endpoints included in this package".PHP_EOL; |
| 124 | + echo " update : Update package to the latest stable version available".PHP_EOL; |
| 125 | + echo " revert : Revert package to a specified version".PHP_EOL; |
| 126 | + echo " delete : Delete package from this system".PHP_EOL; |
| 127 | + echo " backup : Create a backup of the API configuration".PHP_EOL; |
| 128 | + echo " restore : Restore the API configuration from the latest backup".PHP_EOL; |
| 129 | + echo PHP_EOL; |
| 130 | +} |
| 131 | + |
| 132 | +# MAKEENDPOINTS COMMAND |
| 133 | +if (in_array($argv[1], ["buildendpoints"])) { |
| 134 | + build_endpoints(); |
| 135 | +} |
| 136 | +# BACKUP COMMAND |
| 137 | +elseif (in_array($argv[1], ["backup"])) { |
| 138 | + backup(); |
| 139 | +} |
| 140 | +# RESTORE COMMAND |
| 141 | +elseif (in_array($argv[1], ["restore"])) { |
| 142 | + restore(); |
| 143 | +} |
| 144 | +# UPDATE COMMAND |
| 145 | +elseif (in_array($argv[1], ["update"])) { |
| 146 | + update(); |
| 147 | +} |
| 148 | +# REVERT COMMAND |
| 149 | +elseif (in_array($argv[1], ["revert"])) { |
| 150 | + revert($argv[2]); |
| 151 | +} |
| 152 | +# DELETE COMMAND |
| 153 | +elseif (in_array($argv[1], ["delete"])) { |
| 154 | + delete(); |
| 155 | +} |
| 156 | +# VERSION COMMAND |
| 157 | +elseif (in_array($argv[1], ["version"])) { |
| 158 | + version(); |
| 159 | +} |
| 160 | +# HELP COMMAND |
| 161 | +elseif (in_array($argv[1], ["help", null])) { |
| 162 | + help(); |
| 163 | + |
| 164 | +} |
| 165 | +# UNKNOWN COMMAND/DEFAULT |
| 166 | +else { |
| 167 | + echo "Unknown command".PHP_EOL.PHP_EOL; |
| 168 | + help(); |
| 169 | +} |
0 commit comments