Wikipedia:WikiProjekt SVG/Stand/fnc.php

aus Wikipedia, der freien Enzyklopädie
Zur Navigation springen Zur Suche springen
<?PHP
        mysql_connect("localhost", "root", ""); mysql_select_db("wikipedia");
 
        /** 
         * ressource connect( void );
         * Connecting Function
         * Used when script needs a connection to Wikipedia
        */
        $status = '';
        function connect() {
                $fp = fsockopen ("de.wikipedia.org", 80, $errno, $errstr, 30);
                if (!$fp) {
                        echo "$errstr ($errno)<br />\n";
                        exit;
                }
                return $fp;
        }
 
        /** 
         * void savecookie( string cookie_name, string cookie_value );
         * Saves a Cookie into MySQL Database
        */
        function savecookie($cookie_name, $cookie_value) {
                $result = mysql_query("SELECT * FROM `cookies` WHERE `cookie_name` = '".$cookie_name."';");
                if(mysql_num_rows($result) != 0) {
                        mysql_query("DELETE FROM `cookies` WHERE `cookie_name` = '".$cookie_name."';");
                        mysql_query("INSERT INTO `cookies` SET `cookie_name` = '".$cookie_name."', `cookie_value` = '".$cookie_value."';");
                        echo mysql_error();
                } else {
                        mysql_query("INSERT INTO `cookies` SET `cookie_name` = '".$cookie_name."', `cookie_value` = '".$cookie_value."';");
                }
        }
 
        /** 
         * void parsecookie( string http_string );
         * Extract Cookie-Datas and saves them with savecookie()
        */
        function parsecookies($content) {
                preg_match_all("^Set-Cookie: (.*?)=(.*?);^", $content, $cookies);
                for($x = 0; $x < count($cookies[1]); $x++) {
                        savecookie($cookies[1][$x], $cookies[2][$x]);
                }
                status("Alle Cookies aktualisiert.");
        }
 
        /** 
         * int getlastactivity( void );
         * Returns last activity from bot (phantom cookie = time)
        */
        function getlastactivity() {
                $result = mysql_query("SELECT * FROM `cookies` WHERE `cookie_name` = 'time';");
                while($data = mysql_fetch_object($result)) 
                        return $data->cookie_value;
        }
 
        /** 
         * string getcookies( void );
         * Returns all saved cookies, seperated by ; - startet with Cookie:
        */
        function getcookies() {
                $return = 'Cookie: ';
                $result = mysql_query("SELECT * FROM `cookies`;");
                while($data = mysql_fetch_object($result)) $return .= $data->cookie_name."=".$data->cookie_value."; ";
                return substr($return, 0, (strlen($return)-2));
        }
 
        /** 
         * array getcontent( var title );
         * Get the content of an article and the saving attributes
         * array ( "text" => content of article with name title, "attr" => attributes )
        */
        function getcontent($title) {
                status("Hole den Artikel ".$title);
                $fp = connect();
                fputs ($fp, "GET /w/index.php?title=".$title."&action=edit HTTP/1.1\r\nHost: de.wikipedia.org\r\nUser-Agent: Mozilla/5.0 (X11; U; Linux i686; de; rv:1.8.1.6) Gecko/20060601 Firefox/2.0.0.6 (Ubuntu-edgy) Web-Sniffer/1.0.24\r\n".getcookies()."\r\n\r\n");
                $content = '';
                while (!feof($fp)) {
                        $content .= fgets($fp,128);
                }
                parsecookies($content);
 
                fclose($fp);
 
                $pattern = '^type=';
                $pattern .= "'hidden' ";
                $pattern .= 'value="(.*?)" name="wp(.*?)"^';
                $pattern2 = '^name="wp(.*?)" type="hidden" value="(.*?)"^';
 
                preg_match_all($pattern,$content,$items);
                preg_match_all($pattern2,$content,$items2);
 
                $p = strpos($content, '<textarea');
                $k = substr($content, $p, strpos($content, '</textarea>') - $p);
                $k = substr($k, strpos($k, '>') + 1);
                $text = html_entity_decode_utf8($k, ENT_QUOTES);
                status("Artikel $status geholt, analysiert.");
                return array("text" =>$text, "attr" => array("start" => $items[1][0], "end" => $items[1][1], "EditToken" => $items[1][4], "AutoSummary" => $items[2][0]));
        }
 
        /** 
         * void login( string nickname, string password );
         * Login with nickname & password
        */
        function login($nickname, $password) {
                /** api **/
                status("Logge mich als $nickname ein.");
                $fp = connect();
                $data = 'lgname='.$nickname.'&lgdomain=de&lgpassword='.$password;
                $str = "POST /w/api.php?action=login HTTP/1.1\r\nHost: de.wikipedia.org\r\nUser-Agent: Mozilla/5.0 (X11; U; Linux i686; de; rv:1.8.1.6) Gecko/20060601 Firefox/2.0.0.6 (Ubuntu-edgy) Web-Sniffer/1.0.24\r\nContent-Type: application/x-www-form-urlencoded\r\nContent-Length: ".strlen($data)."\r\n\r\n".$data."\r\n\r\n";
                //echo $str;
                fputs($fp, $str);
                $content = '';
                while (!feof($fp)) {
                        $content .= fgets($fp,128);
                }
                //echo $content;
                parsecookies($content);
                fclose($fp);
                
                /** regular **/
                $fp = connect();
                $data = 'wpName='.$nickname.'&wpPassword='.$password.'&wpRemember=1&wpLoginattempt=Anmelden';
 
                fputs($fp, "POST /w/index.php?title=Spezial:Anmelden&action=submitlogin&type=login HTTP/1.1\r\nHost: de.wikipedia.org\r\nUser-Agent: Mozilla/5.0 (X11; U; Linux i686; de; rv:1.8.1.6) Gecko/20060601 Firefox/2.0.0.6 (Ubuntu-edgy) Web-Sniffer/1.0.24\r\nContent-Type: application/x-www-form-urlencoded\r\nContent-Length: ".strlen($data)."\r\n\r\n".$data."\r\n\r\n");
                $content = '';
                while (!feof($fp)) {
                        $content .= fgets($fp,128);
                }
                //echo $content;
                parsecookies($content);
                fclose($fp);
                status("Ich bin nun $nickname.");
        }
 
        /** 
         * void savecontent( string title, string text, array attr, string summary );
         * Saves an article with title, text and summary
        */
        function savecontent($title, $text, $attr, $summary) {
                status("Sende veränderten Artikel $title an Wikipedia.");
                $fp = connect();
 
                $data = '?wpSection=&wpStarttime='.$attr['start'].'&wpEdittime='.$attr['end'];
                $data .= '&wpScrolltop=0&wpTextbox1='.urlencode($text).'&wpSummary='.$summary.'&wpWatchthis=1&wpSave=Seite+speichern';
                $data .= '&wpEditToken='.urlencode($attr['EditToken']).'&wpAutoSummary='.urlencode($attr['AutoSummary']);
                fputs($fp, "POST /w/index.php?title=".$title."&action=submit HTTP/1.1\r\nHost: de.wikipedia.org\r\nUser-Agent: Mozilla/5.0 (X11; U; Linux i686; de; rv:1.8.1.6) Gecko/20060601 Firefox/2.0.0.6 (Ubuntu-edgy) Web-Sniffer/1.0.24\r\n".getcookies()."\r\nContent-Type: application/x-www-form-urlencoded\r\nContent-Length: ".strlen($data)."\r\n\r\n".$data."\r\n\r\n");
                $content = '';
                while (!feof($fp)) {
                        $content .= fgets($fp,128);
                }
 
                if(strstr($content, "Bearbeitungskonflikt"))
                        status("<font color='red'><b>Bearbeitungskonflikt festgestellt</b></font> - Mir wurde noch nicht beigebracht, bei solche fällen zu handeln! Informiere meinen Besizter!");
 
                parsecookies($content);
                fclose($fp);
                status("Neue Version gespeichert.");
        }
        
        /** list things of category **/
        function list_category($category_name) {          
          $fp = connect();
          fputs ($fp, "GET /w/api.php?action=query&prop=categories&cmtitle=".$category_name."&list=categorymembers&cmlimit=5000&format=xml HTTP/1.1\r\nHost: de.wikipedia.org\r\nUser-Agent: Mozilla/5.0 (X11; U; Linux i686; de; rv:1.8.1.6) Gecko/20060601 Firefox/2.0.0.6 (Ubuntu-edgy) Web-Sniffer/1.0.24\r\n".getcookies()."\r\n\r\n");
          $content = '';
          
          while (!feof($fp)) {
            $content .= fgets($fp,128);
          }
          parsecookies($content);

          fclose($fp);
          
          $pattern = '~<cm pageid="(.*?)" ns="6" title="(.*?)" />~is';
          //
          // RegExp auswerten
          //
          preg_match_all($pattern, $content, $subpattern); 
          
          return($subpattern[2]);
        }
        
        /** check usage of image **/
        function check_usage($image) {
          $fp = connect();
          fputs ($fp, "GET /w/api.php?action=query&list=imageusage&iutitle=".urlencode($image)."&format=xml HTTP/1.1\r\nHost: de.wikipedia.org\r\nUser-Agent: Mozilla/5.0 (X11; U; Linux i686; de; rv:1.8.1.6) Gecko/20060601 Firefox/2.0.0.6 (Ubuntu-edgy) Web-Sniffer/1.0.24\r\n".getcookies()."\r\n\r\n");
          $content = '';
          
          while (!feof($fp)) {
            $content .= fgets($fp,128);
          }
          parsecookies($content);

          fclose($fp);
          
          $pattern = '~<iu(.*?)title="(.*?)"(.*?)>~is';
          //
          // RegExp auswerten
          //
          preg_match_all($pattern, $content, $subpattern); 
          
          return($subpattern[2]);
        }
        /** 
         * void status( status text );
         * Add status message into status variable
        */
        function status($text) {
                global $status;
                $status .= "<font face=\"Courier New\" size=\"2\"><b>[".date("H:i:s")."]</b> $text</font><br>";
        }
 
        /** 
         * void ...( text );
         * HTML entity decode to utf8 (html_entity_decode does not work well)
        */
        function html_entity_decode_utf8($string)
        {
                        static $trans_tbl;
 
                        // replace numeric entities
                        $string = preg_replace('~&#x([0-9a-f]+);~ei', 'code2utf(hexdec("\\1"))', $string);
                        $string = preg_replace('~&#([0-9]+);~e', 'code2utf(\\1)', $string);
 
                        // replace literal entities
                        if (!isset($trans_tbl))
                        {
                                        $trans_tbl = array();
 
                                        foreach (get_html_translation_table(HTML_ENTITIES) as $val=>$key)
                                                        $trans_tbl[$key] = utf8_encode($val);
                        }
 
                        return strtr($string, $trans_tbl);
        }
?>