Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/256.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Twitch.tv上Nghtbot的自定义API-PHP命令出现问题 调用quote时是否超过400个字符?远远少于400个字符。更不用说了。也在发行!Quote ADD应该小于400,直到我放置要添加的文本。 <?php /* _Php_Api - Fatal编程技术网

Twitch.tv上Nghtbot的自定义API-PHP命令出现问题 调用quote时是否超过400个字符?远远少于400个字符。更不用说了。也在发行!Quote ADD应该小于400,直到我放置要添加的文本。 <?php /*

Twitch.tv上Nghtbot的自定义API-PHP命令出现问题 调用quote时是否超过400个字符?远远少于400个字符。更不用说了。也在发行!Quote ADD应该小于400,直到我放置要添加的文本。 <?php /* ,php,api,Php,Api,Twitch.tv上Nghtbot的自定义API-PHP命令出现问题 调用quote时是否超过400个字符?远远少于400个字符。更不用说了。也在发行!Quote ADD应该小于400,直到我放置要添加的文本。 <?php /* AUTHOR: The Wacky Viking FILE: quote.php ### INSTALLATION ### To use this api, you have to hos

Twitch.tv上Nghtbot的自定义API-PHP命令出现问题
调用quote时是否超过400个字符?远远少于400个字符。更不用说了。也在发行!Quote ADD应该小于400,直到我放置要添加的文本。
<?php
    /*
        AUTHOR: The Wacky Viking
        FILE: quote.php

        ### INSTALLATION ###

        To use this api, you have to host it on a public webserver.
        This API will work off of ONE nightbot command, but it is recommended to split up. *See SUGGESTIONS

        1) Save the file someplace on the server. PS: You can rename "quote.php" to anything you want.
        2) Make sure the file has permission to read/write files
        3) To add the API to nightbot, add this command;

                !addcom !quote $(customapi https://HOST/PATH/quote.php?$(query)) 

            The above command will let ANY type of user use all of the Quotes commands.
        4) Run the Quote.php-file once from webbrowser.

        If you have not specified a filename in CONFIGURATION, it will default to "quotes.txt".
        If the program can't find the file, it will attempt to create it for you.

        ### USAGE ###

        !quote                       : Displays random quote
        !quote sel <INDEX>           : Displays specific quote with INDEX.

        !quote add <quote>           : Adds a new quote <quote> to the list. Suggested format "<quote>" - @<user>, <year/date/situation>

        !quote list                  : Provides a URL for you to see all the quotes and their INDEX
        !quote edit <INDEX> <quote>  :  Overwrites quote with INDEX. Edited quote will be <quote>.
        !quote rem <INDEX>           : Removes quote with INDEX. Fetch the index at 

        ### SUGGESTIONS ###

        I suggest having the administative commands(add, remove, edit) and the public RANDOM_QUOTE and LIST on two separate commands. 
        This is in order to prevent spamming new/dumb quotes, or griefers removing or editing quotes.

        Ex. !addcom !quotes -ul=mod/reg/owner $(customapi https://HOST/PATH/quote.php?ARG=$(query)) This url should work with all of the features trusted people.
            !addcom !quote $(costomapi https://HOST/PATH/quote.php) <-- Only displays random quote for regular users.

    */
    $GLOBALS['DEBUG'] = false; // Use this when you are testing, to get an output of which branch is acting, and which it is SUPPOSED to do.
    $GLOBALS['DEBUG_COMM'] = false; // Use this to get DEBUG output from 

    // CONFIGURATION 
    $filename = "quotes.txt"; // Name of the textfile to store Quotes in. Name it whatever you want.
        // Yes, this is all the config needed to run this API! =D Dynamic coding FTW!


    // - CHOOSE AVAILABLE FUNTIONALITY SERVER-SIDE
    $ALLOW_REMOVE = true; // If set to "false", you can only remove quotes directly in textfile.
    $ALLOW_EDIT = true; // If set to "false", you can only edit quotes directly in textfile.
    $ALLOW_LIST = true; // If set to "false", you can not list quotes. -- For server security.
    $ALLOW_ADD = true; // If set to "false", you can only add quotes directly in textfile.
    $ALLOW_SELECT = true; // If set to "false", you cannot select specific quote.

    $input = ValidateInput();
    $com = substr($input, 0, 4);

     // $URL = returnDir();

     // echo $URL;


    $quotes = array();


    // Script start
    ValidateFilename();
    Initialize();

    switch($com){
        case 'ADD ':
            if(!$GLOBALS['ALLOW_ADD']) exit("Error: Adding quotes is not enabled in config.");
            $com = substr($input, 0, 3); // TODO : Must add "(<id>) - " as prefix.
            $arg = substr($input, 4);
            if($GLOBALS['DEBUG_COMM']) echo "DEBUG: Current command: ADD(" . $com . ") ARG: " . $arg;

            quoteAdd($arg);
            break;
        case 'REM ':
            if(!$GLOBALS['ALLOW_REMOVE']) exit("Error: Removing quotes is not enabled in config.");
            $com = substr($input, 0, 3);
            $arg = substr($input, 4);
            if($GLOBALS['DEBUG_COMM']) echo "DEBUG: Current command: REM(" . $com . ") ARG: " . $arg;
            quoteRemove($arg);
            break;
        case 'EDIT':
        if(!$GLOBALS['ALLOW_EDIT']) exit("Error: Editing quotes is not enabled in config.");
            $com = substr($input, 0, 4);
            $arg = substr($input, 5);
            if($GLOBALS['DEBUG_COMM']) echo "DEBUG: Current command: EDIT(" . $com . ") ARG: " . $arg;
            quoteEdit($arg);
            break;
        case 'LIST':
            if(!$GLOBALS['ALLOW_LIST']) exit("Error: Adding is not enabled in config.");
            $com = substr($input, 0, 4);
            $arg = substr($input, 5);
            if($GLOBALS['DEBUG_COMM']) echo "DEBUG: Current command: LIST(" . $com . ") ARG: " . $arg; // No need to actually process arguments here. It will only provide a link to the quotes-list.
            quoteList();
            break;
        case 'SEL ':
            if(!$GLOBALS['ALLOW_SELECT']) exit("Error: Adding is not enabled in config.");
            $com = substr($input, 0, 3);
            $arg = substr($input, 4);
            if($GLOBALS['DEBUG_COMM']) echo "DEBUG: Current command: SELECT(" . $com . ") ARG: " . $arg;
            quoteSelect($arg);
            break;
        default:
            $com = $input;
            $arg = $com;
            if($GLOBALS['DEBUG_COMM']) echo "DEBUG: Current command: DEFAULT(" . $com . ") ARG: " . $arg; // Displays when no other command is given. In this case, outputs a random quote.
            RandomQuote();
            break;
    }
    UpdateSavefile();

    function quoteAdd($toAdd){ // Update this code to add to array instead of the file itself. Use UpdateSavefile
        $str = $toAdd;
        $cnt = count($GLOBALS['quotes']) + 1;
        $prefix = "(" . $cnt . ") - ";

        // New code, adds quote to Array, which in turn updates the savefile
        if(array_push($GLOBALS['quotes'], $prefix . $str . "\n")){
            echo "Quote added!";
        } else {
            echo "ERROR: Quote was not added!";
        }
    }
    function quoteRemove($id){ // TODO: Test this command properly.
        // TODO: Add this function
        // TODO: Make sure to refresh all INDEX-es in the textfile.
        // IDEA: Don't mess with the array. As the array loads in from file
        // every run, you can just find a way to remove one line of text
        // matching the INDEX($id) provided from the textfile.

        // Better idea: Remove from array, and replace the text-file with a looped through array
        $tmp = array();
        $toRemove = "(". $id . ") - ";
        $lines = $GLOBALS['quotes'];
        foreach($lines as &$value){
            if(strpos($value, $toRemove) !== false){
                if($GLOBALS['DEBUG']) echo "This branch should never be accessed.";
            } else {
                $toAdd = StripQuote($value);
                $cnt = (count($tmp)+1);
                $prefix = "(" . $cnt . ") - ";
                $output = $prefix . $toAdd;
                if(array_push($tmp, $output)){

                // TODO: Check if you need to strip item here and re-set the INDEX-tag.
                    if($GLOBALS['DEBUG']) echo "Added value to array...";
                }
            }
        }
        //$GLOBALS['quotes'] = array();

        $GLOBALS['quotes'] = $tmp; // Push items into the quotes array. Might need to strip them first.


    }
    function quoteList(){

        echo "Check out all the quotes here: " . $GLOBALS['URL'] . $GLOBALS['filename'];
    }
    function quoteSelect($id){
        echo StripQuote($GLOBALS['quotes'][$id-1]);
    }
    function quoteEdit($arg){
        // TODO: Have to explode this into to different variables. One for INDEX, one for QUOTE

        //echo $arg;
        $tmp = explode(" ", $arg, 2);

        $id = $tmp[0];
        $quote = $tmp[1] . "\n";
        echo "ID: " . $id . " - QUOTE: " . $quote;
        $output = "(" . $id . ") - " . $quote;
        $GLOBALS['quotes'][$id-1] = $output;
    }
    function RandomQuote(){

        if($GLOBALS['DEBUG']) echo "Selecting random quote...";
        $r = rand(0, count($GLOBALS['quotes'])-1);
        $current = $r + 1;
        //if($r <= 0) $r = 0; // Test this line
        $str = $GLOBALS['quotes'][$r];
        echo StripQuote($str);
    }
    function LoadQuotes(){
        if($GLOBALS['DEBUG']) echo "Loading in Quotes from savefile...";
        $file = fopen($GLOBALS['filename'], "r") or die('Unable to open "'. $GLOBALS['$filename'] .'". Make sure the file exists.'); // The file should always exist, as we create it if it don't every time we run the Initialize routine.

        while(! feof($file)){
            $str = fgets($file);
            if($str != "") {
                array_push($GLOBALS['quotes'], $str);
            }
        }
        fclose($file);
    }
    function StripQuote($toStrip){
        // Strips a quote of its "(1)" index tag.
        $start = strpos($toStrip, '"'); // Fetches the Index of the needle(here: ").
        return substr($toStrip, $start); // Removes the unesessary indexing.
    }
    function ValidateInput(){
        if($GLOBALS['DEBUG']) echo "Validating input...";
        if(isset($_GET['arg'])){
            if($GLOBALS['DEBUG']) echo "Input OK!";
            return $_GET['arg'];
        } else {
            return "Please specify 'arg' in URL.";
        }
    }
    function ValidateFilename(){
        // Check if filename is not empty. If it is, create a name.
        if($GLOBALS['DEBUG']) echo "Filename: " . $GLOBALS['filename'];

        if($GLOBALS['filename'] == ""){
            $GLOBALS['filename'] = "quotes.txt"; // <-- Default filename. Triggers if no custom filename is given.
        } 
    }
    function UpdateSavefile(){
        // TODO: Push array data into save file. This is the only function 
        // that should interact with the savefile!
        file_put_contents($GLOBALS['filename'], "");
        foreach($GLOBALS['quotes'] as &$value){
            if(file_put_contents($GLOBALS['filename'], $value, FILE_APPEND)){
                if($GLOBALS['DEBUG']) echo "Updating savefile...success!";
            } else {
                if($GLOBALS['DEBUG']) echo "Updating savefile...failed!";
            }
        }
    }
    function Initialize(){
        if($GLOBALS['DEBUG']) echo "Initializing Quotes API...";

        // Run this command every time the page loads
        // in order to check if "quotes.txt" exists.
        if(!file_exists($GLOBALS['filename'])){
            // Create file if not already exist.
            $GLOBALS['filename'] = fopen($GLOBALS['filename'], "w");
            if($GLOBALS['DEBUG']) echo "File did not exist. Created file...";
        }

        // Load quotes from file into the array.
        LoadQuotes();
    }
    function returnDir(){
        $input = $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
        $tmp = explode("/", $input);
        $output = "";
        foreach ($tmp as &$value) {
            if($value != $tmp[count($tmp)-1])
            $output = $output . $value . "/";
        }
        return $output;
    }
?>