Issue

* the POST request is not working, it doesn't update content to filtered content.


Example Use case

* Pick http://nolimitswebdesign.com.dnnmax....4/Default.aspx
* Check content under " Edit Text/HTML" after you click "Basic Text Box"
* The text specific inside <a> tags there is also specific in bad.txt, e.g. it has to be replaced with #
* Script works fine, but the last post doesn't update content of the text area. Any suggestions


Code:
001.<?php
002. 
003.// get arrays of bad worlds
004.$urls = file("urls.txt");
005.$badWords = file("bad.txt");
006. 
007. 
008.// loop over every url...
009.foreach($urls as $url) {
010. 
011.    // get rid of any whitespace of linebreaks
012.    $url = trim($url);
013. 
014.    if(!startsWith($url, "#")) //# is used to comment any inactive urls
015.    {
016. 
017.        echo "<BR />\n Sending Request to ". $url;
018.        // sending a get request using curl
019.        $getPage = sendCurl( $url, false, "" );
020.        echo "<BR />\n Got Response";
021. 
022. 
023.        // Check for possible errors
024.        if ( $getPage['errno'] != 0 )
025.            echo"<BR />\n Could not get Page: Error => bad url | timeout | redirect loop";
026. 
027.        else if ( $getPage['http_code'] != 200 )
028.            echo"<BR />\n Could not get Page : Error => no page | no permissions | no service";
029. 
030.        else {
031.            echo "<BR />\n No Errors Encountered. Successfully fetched page";
032. 
033.            // get page content
034.            $page = $getPage['content'];
035. 
036. 
037. 
038. 
039.            //echo "<BR />\n Extracted View State:<BR />\n" . $viewstate;
040.            //echo "<BR />\n";
041.            $viewstate = getVStateContent($page);
042. 
043. 
044. 
045.            // extracting data from textarea
046.            $content = getTAreaContent($page);
047.            //echo "<BR />\n Extracted Content:<BR />\n" . $content;
048.            //echo "<BR />\n";
049. 
050. 
051.            // check if extracted data contains any bad words
052.            foreach($badWords as $badWord) {
053. 
054.                // get rid of any whitespace of linebreaks
055.                $badWord = trim($badWord);
056. 
057.                //echo "<BR />\nTesting for : " . $badWord;
058.                //echo "<BR /><BR />\n\nstrstr(\"$content\",\"$badWord\")";
059. 
060.                // check if selected bad word exists in content, if yes, replace it
061.                if(strstr($content, $badWord)) {
062.                    //echo "Found it";
063.                    $content = str_replace($badWord, "#", $content);
064.                }
065.            }
066. 
067.        //echo "<BR /><BR />\n\n New Content:<BR />\n" . $content;
068.        //echo "<BR /><BR />\n\n";
069. 
070. 
071.        $postContent = getPostArray($viewstate, $content);
072.        echo "<BR /><BR />\n\n Post Content:<BR />\n";
073.        print_r($postContent);
074. 
075. 
076. 
077. 
078.        echo "<BR /><BR />\n\n";
079.        echo "<BR />\n Sending Postback to : " . $url;
080.        $postbackPageCurl = sendCurl( $url, true, $postContent );
081. 
082.        // Check for possible errors
083.        if ( $postbackPageCurl['errno'] != 0 )
084.            echo"<BR />\n Could not send postback : Error => bad url | timeout | redirect loop";
085. 
086.        else if ( $postbackPageCurl['http_code'] != 200 )
087.            echo"<BR />\n Could not send postback : Error: no page | no permissions | no service";
088. 
089.        else {
090. 
091.                echo "<BR />\n Postback was sent successfuly";
092.                echo "<BR />\n";
093. 
094. 
095. 
096.                // get page content, this should contain the filtered content and "Basic Text Box" checked
097.                $postbackPage = $postbackPageCurl['content'];
098.                //echo "<BR /><BR />\n\nPage content:<BR/><BR/>\n\n";
099.                //echo $postbackPage;
100. 
101.                echo "<BR /><BR />\n\n";
102.                echo "Returned content after Post is <BR/>\n";
103.                echo getTAreaContent($postbackPage);
104.                echo "<BR /><BR />\n\n";
105.            }
106. 
107.        }
108. 
109.    }
110. 
111. 
112.}
113. 
114. 
115. 
116.// This function extracts and returns the text written inside text area from page's content
117.function getTAreaContent($data) {
118.    $contentStartTag = "wrap=\"virtual\">";
119.    $contentEndTag = "</textarea>";
120.    $contentStartIndex = strpos($data, $contentStartTag) +15;
121.    $contentEndIndex = strpos(substr($data, $contentStartIndex), $contentEndTag);
122. 
123.    // extracting data from textarea
124.    $content = substr($data, $contentStartIndex, $contentEndIndex);
125. 
126.    return $content;
127. 
128.}
129. 
130. 
131.// This function extracts and returns __VIEWSTATE from a page's content
132.function getVStateContent($data) {
133. 
134.    $viewstateStartTag = "<input type=\"hidden\" name=\"__VIEWSTATE\" id=\"__VIEWSTATE\" value=\"";
135.    $viewstateEndTag = "\" />";
136.    $viewstateStartIndex = strpos($data, $viewstateStartTag) + 64;
137.    $viewstateEndIndex = strpos(substr($data, $viewstateStartIndex), $viewstateEndTag);
138.    $viewstate = substr($data, $viewstateStartIndex, $viewstateEndIndex);
139. 
140.    return $viewstate;
141.}
142. 
143. 
144. 
145. 
146. 
147.// This function return POSTARGS that are meant to be sent to the page within post request
148.function getPostArray($viewstate, $content) {
149. 
150.    $postContent["__EVENTTARGET"] = "dnn\$ctr374\$EditHTML\$teContent\$optView\$0";
151.    $postContent["__EVENTARGUMENT"] = "";
152.    $postContent["__LASTFOCUS"] = "";
153.    $postContent["__VIEWSTATE"] = $viewstate;
154.    $postContent["dnn\$dnnSEARCH\$txtSearchNew"] ="";
155.    $postContent["dnn\$ctr374\$EditHTML\$teContent\$optView"] = "RICH";
156.    $postContent["dnn\$ctr374\$EditHTML\$teContent\$txtDesktopHTML"] = $content;
157.    $postContent["dnn\$ctr374\$EditHTML\$txtDesktopSummary"] = "";
158.    $postContent["ScrollTop"] ="";
159.    $postContent["dnn\$ctr374\$EditHTML\$teContent\$optRender"] = "T";
160.    $postContent["dnn\$IconBar.ascx\$optMode"] = "VIEW";
161.    $postContent["__dnnVariable"] = "SearchIconWebUrl:url(/images/Search/google-icon.gif);".                                        "SearchIconSiteUrl:url(/images/Search/dotnetnuke-icon.gif);".
162.                                            "SearchIconSelected:S";
163. 
164. 
165.    return $postContent;
166.}
167. 
168. 
169. 
170. 
171. 
172.function startsWith($haystack,$needle,$case=true) {
173.    if($case){return (strcmp(substr($haystack, 0, strlen($needle)),$needle)===0);}
174.    return (strcasecmp(substr($haystack, 0, strlen($needle)),$needle)===0);
175.}
176. 
177.function endsWith($haystack,$needle,$case=true) {
178.    if($case){return (strcmp(substr($haystack, strlen($haystack) - strlen($needle)),$needle)===0);}
179.    return (strcasecmp(substr($haystack, strlen($haystack) - strlen($needle)),$needle)===0);
180.}
181. 
182. 
183. 
184.// This function sends GET/POST requests using curl
185.function sendCurl( $url, $ispost=false, $postData="", $cookie="" )
186.{
187. 
188.    $options = array(
189.        CURLOPT_RETURNTRANSFER => true,             // return web page
190.        CURLOPT_HEADER         => false,            // don't return headers
191.        CURLOPT_FOLLOWLOCATION => true,             // follow redirects
192.        CURLOPT_ENCODING       => "",               // handle all encodings
193.        CURLOPT_AUTOREFERER    => true,             // set referer on redirect
194.        CURLOPT_CONNECTTIMEOUT => 120,              // timeout on connect
195.        CURLOPT_TIMEOUT        => 120,              // timeout on response
196.        CURLOPT_MAXREDIRS      => 10,               // stop after 10 redirects
197.        CURLOPT_USERAGENT      => "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)",
198.        CURLOPT_REFERER        => $url,
199.        CURLOPT_HTTPHEADER     => array(
200.            "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
201.            "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7",
202.            "Accept-Encoding: gzip,deflate",
203.            "Accept-Language: en-us,en;q=0.5",
204.            "Connection: Keep-Alive",
205.            "Content-Type: text/xml; charset=utf-8",
206.            "Expect: 100-continue",
207.            "Keep-Alive: 115")
208.                         // These headers were extracted from a DNN POST request using Firefox's Live HTTP Headers plugin
209. 
210.    );
211. 
212. 
213.    if($ispost) {
214.        $options[CURLOPT_POST] = 1;                 // its a post request
215.        $options[CURLOPT_POSTFIELDS] = $postData;   // data for post request
216.    }
217. 
218. 
219. 
220. 
221.    $ch      = curl_init( $url );
222.    curl_setopt_array( $ch, $options );
223.    $content = curl_exec( $ch );
224.    $err     = curl_errno( $ch );
225.    $errmsg  = curl_error( $ch );
226.    $header  = curl_getinfo( $ch );
227.    curl_close( $ch );
228. 
229.    $header['errno']   = $err;
230.    $header['errmsg']  = $errmsg;
231.    $header['content'] = $content;
232.    return $header;
233.}
234. 
235.?>