LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Networking
User Name
Password
Linux - Networking This forum is for any issue related to networks or networking.
Routing, network cards, OSI, etc. Anything is fair game.

Notices


Reply
  Search this Thread
Old 05-09-2016, 01:06 AM   #1
AhamedSajid
LQ Newbie
 
Registered: May 2016
Posts: 2

Rep: Reputation: Disabled
Smile How to connect squid.conf file to PHP / HTML page and give read write access


I have already configured squid proxy server to my LAN setup and its running well but now i want to connect the squid.conf file which is in the Linux server to a PHP or HTML page which currently running through Linux LAMP server, and give read and write access to the squid.conf file from the PHP page? i want the squid configurations to be edited , saved and restarted from the PHP page itself.

Please help me out if there is a specific coding to be written in PHP / HTML for the connection and the methods.
 
Old 05-09-2016, 01:49 AM   #2
Elizine
Member
 
Registered: Aug 2015
Posts: 54
Blog Entries: 1

Rep: Reputation: Disabled
How to connect squid.conf file to PHP / HTML page and give read write access

Code:
# determine which URLs are going to be caught
acl youtube dstdomain .youtube.com

# pass requests 
url_rewrite_program /etc/squid/phpredir.php
url_rewrite_access allow youtube

# leave caching up to the local web server
cache deny youtube
/usr/bin/php may not be the correct path or name for PHP on your system. Be sure to check and update this following example as needed.
Also take care to remove the space between # !. It is there to avoid a wiki bug.

phpredir.php:

Code:
# !/usr/bin/php
<?php

while ( $input = fgets(STDIN) ) {
  // Split the output (space delimited) from squid into an array.
  $input=explode(" ",$input);
  if(preg_match("@youtube@",$input[0])){
        $input[0]=urlencode($input[0]);
        $input= implode(" ",$input);
        echo "http://10.13.37.25/per.php?url=$input"; //URL of my web server
  }else
        echo ""; // empty line means no re-write by Squid.
}
?>
per.php:
Code:
<?php
        
        $file_path="/var/www/videos";
        $logfile="$file_path/cache.log";
        $url=urldecode($_GET['url']);
        $urlptr=fopen($_GET['url'],"r");
        $blocksize=32*1024;
        
        //attempt to get. a 404 shouldn't happen, but...
        if($urlptr===FALSE){
                header("Status: 404 Not Found");
                die();
        }
        
        //find content type and length
        foreach($http_response_header as $line){
                if(substr_compare($line,'Content-Type',0,12,true)==0)
                        $content_type=$line;
                else if(substr_compare($line,'Content-Length',0,14,true)==0){
                        $content_length=$line;
                }
        }
        
        
        /**Youtube will detect if requests are coming form the wrong ip (ie, if only video requests are redirected, so, we must redirect all requests to youtube.
        As such, we must capture all requests t youtube. Most are unimportant, so we can pass them straight through **/
        if(!preg_match("@.*youtube.*videoplayback.*@",$url)){
                fpassthru($urlptr);
                fclose($urlptr);
                exit(0);
        } 
        
        //send content type and length
        header($content_type);
        header($content_length);
        
        //find youtube id;
        $url_exploded=explode('&',$url);
        $id="";
        foreach($url_exploded as $line){
                if(substr($line,0,3)==='id=')
                        $id=substr($line,3);
        }
        //Get the supposed file size
        $length=intval(substr($content_length,16));
        file_put_contents($logfile,"\nFound id=$id, content-type: $content_type content-length=$content_length\n",FILE_APPEND);
        
        //Do we have it? delivar if we do
        $fname="$file_path/$id-$length";
//Check if we have the file, and it is the correct size. incorrect size implies corruption
        if(file_exists($fname) &&filesize($fname)==$length){
                readfile($fname);
                logdata("HIT",$url,$fname);
                exit(0);
        }
        
        //file not in cache? Get it, send it & save it
        logdata("MISS",$url,$fname);
        $fileptr=fopen($fname,"w");
        //no validity check, simply don't write the file if we can't open it. prevents noticeable failure/
        
        while(!feof($urlptr)){
                $line=fread($urlptr,$blocksize);
                echo $line;
                if($fileptr) fwrite($fileptr,$line);
        }
        fclose($urlptr);
        if($fileptr) fclose($fileptr);
        
        function logdata($type,$what, $fname){
        $file_path="/var/www/videos";
        $logfile="$file_path/cache.log";
                $line="@ ".time()."Cache $type url: $what file: $fname client:".$_SERVER['REMOTE_ADDR']."\n";
                file_put_contents($logfile,$line,FILE_APPEND);
                }
?>
 
1 members found this post helpful.
Old 05-09-2016, 09:29 AM   #3
AhamedSajid
LQ Newbie
 
Registered: May 2016
Posts: 2

Original Poster
Rep: Reputation: Disabled
Thank you ELIZIne i'l try doing this configuration. And check what happens
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Give User Read/Write Access to HTML Folder ep7network0819 Linux - Server 2 05-09-2016 12:16 PM
how to give all users read/write access to a file in the Firefox profile folder 7trek Fedora 2 11-25-2007 06:09 PM
HTML form to read/write from local file mustangfanatic01 Linux - Software 2 03-11-2006 04:29 PM
RSS XML File to be read and generate HTML page redhatrosh General 10 02-15-2006 03:16 AM
How can i read an write to a HTML or xml file using C alix123 Programming 1 11-24-2004 06:07 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Networking

All times are GMT -5. The time now is 03:18 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration