Welcome Guest, Not a member yet? Register   Sign In
[Solved] Website generator
#1

[eluser]Jareish[/eluser]
Hi all,

I'm building a CMS to create websites. Everything works fine, people can input stuff, it comes in the database and I've written the template file to be filled in.

After pressing the "create a zip" button, my builder function starts to run. It basicly pulls a template file (for instance index.html.php) fills in the $data and stores a static html file in a zip.

Code:
$this->zip->add_data('index.html', $this->parse_html('build/html/index.html.php', $data));

    function parse_html($file, $data)
    {
        ob_start();    
        $this->load->view($file, $data);
        $datasource = ob_get_contents();
        ob_end_clean();
        return $datasource;
    }

Now this is where I start having problems.

1) This doesn't really seem a clean method (I have to generate 20 files like this)
2) I alsoo need to copy images and javascript to the zip, these ofcourse don't need to be parsed as a view, but can be copied straight into a zip in a specific directory. How would I do this?

if I use $this->zip->read_file() the file either goes in the root (while I want images in a /images folder) or if oyu pass TRUE, it uses the directory in which the image resides, in this case base_url().'template/images'

So I thought about reading the datastream of that image using file_get_contents and use add_data() so I can specify the output directory. But even though I have fopen and curl enabled on my server, I get a stream error. I tried absolute pathing, relative, inside the view, inside the controller, outside the application, google.com etc file_get_contents/curl/fopen nothing works, all stream errors. IMO a stream error is something you get when he can find the file but isn't allowed to read, so either CI blocks it, or something else fishy is going on, on my server.

my curl function:
Code:
function curl_get_file($filename)
    {
        $ch = curl_init();
        $timeout = 5; // set to zero for no timeout
        curl_setopt ($ch, CURLOPT_URL, $filename);
        curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
        $file_contents = curl_exec($ch);
        curl_close($ch);

        // display file
        return $file_contents;
    }

my file_get_contents function
Code:
function getFile($filename)
    {
        ob_start();    
        file_get_contents($filename);
        $datasource = ob_get_contents();
        ob_end_clean();
        return $datasource;
    }
(not sure if I need the ob_start here).

In short, my question would be, could my html file parser be cleaner (opening views over and over doesn't really seem clean).

And second question, how can I copy images/javascript/flash to the zip while maintaining a specific directory structure.

PS: My goal is to create a html template where I fill in the blanks with php, then output a html with no php in it, so the user can run it locally and on non php servers.
#2

[eluser]dudeami0[/eluser]
For the first chunk of code, change

Code:
function parse_html($file, $data)  {
   ob_start();    
   $this->load->view($file, $data);
   $datasource = ob_get_contents();
   ob_end_clean();
   return $datasource;
}
to
Code:
function parse_html($file, $data)  {
   return $this->load->view($file, $data, true);
}

And the way you have this function wrote wont work (Nothing is echoed or printed so nothing is given :S) Change:

Code:
function getFile($filename) {
   ob_start();    
   file_get_contents($filename);
   $datasource = ob_get_contents();
   ob_end_clean();
   return $datasource;
}
to
Code:
function getFile($filename) {
   return file_get_contents($filename);
}
#3

[eluser]Jareish[/eluser]
Hi,

The parse_html function works

The getFile() doesn't:

the input:
Code:
$this->zip->add_data('js/jquery-1.4.2.min.js',  $this->getFile(base_url().'template/jquery/jquery-1.4.2.min.js'));

the error:
Code:
Message: file_get_contents(http://www.talentstoolkit.com/cms/ci/template/jquery/jquery-1.4.2.min.js) [function.file-get-contents]: failed to open stream: Connection refused

I could use parse_html() to parse the javascript and avoid file_get_contents. But I get a
Code:
Warning: Unexpected character in input: '' (ASCII=11) state=1 in [..]
if I try flash.

Alsoo it seems I can't use file_get_contents on something that resides in ci application folder. Which would mean, part of my template would be in build/view and other part (images/javascript/flash) would be located outside CI in a public folder. If there is no other (quick) way, Ill settle for that, as long as there is a solution for this problem
#4

[eluser]dudeami0[/eluser]
If everything is being pulled locally, just use

Code:
getFile('/location/of/file.ext')

Also, it may be a possiblity your host does not allow outgoing connections.
#5

[eluser]Jareish[/eluser]
Thanks!

I think when I absolute linked to the file, my host saw it as a outgoing connection. Although I tried relative links, I was relative linking from my root and not from codeigniter root.

Code:
$this->zip->add_data('js/jquery-1.4.2.min.js',  $this->getFile('template/jquery/jquery-1.4.2.min.js'));

Works!

Alsoo I checked flash, no problems.

Curious... I now have my assets and my template in a complete different folder. I wonder if I could move my assets to the view folder or my view to the assets folder... brb testing

Edit:
linking to a folder in view works all the same Smile Keeps things nice and clean
Code:
$this->zip->add_data('js/jquery-1.4.23.min.js',  $this->getFile('application/views/build/jquery/jquery-1.4.2.min.js'));




Theme © iAndrew 2016 - Forum software by © MyBB