I am currently developing somthing like a CMS, but without using any database. A really big problem for me is how can you change (and later format content via tinymce (or similar) ) and then save it into a php file?
My configuration file for each page looks like this:
<?php
$title = 'Title of the page';
$contenttitle = <<<HTML
Something like a short summary, displayed at the top
HTML;
$content = <<<HTML
main content
HTML;
$foldername = basename(__FILE__, '.php');
include '../template/standard/layout/index.php'; ?>
Now, I want to be able to open a script in the browser where I can select the file I want to edit and see text boxes with content of $title, $contenttitle and $content.
So, in fact we open a file, look for these 3 variables, display their content and change them (overwrite the file).
How can this be solved?
This is just an example approach that may help you:
Write a php/html file which will print out a form for you:
<form method="post">
<input type="text" name="title" value=""/>
<input type="text" name="contenttitle" value=""/>
<textarea name="content">">
<input type="submit" value="save"/>
</form>
Handle your form in the php file:
if (!empty($_POST)) {
$result = array(
'title' => htmlspecialchars($_POST['title']),
'contenttitle' => htmlspecialchars($_POST['contenttitle']),
'content' => htmlspecialchars($_POST['content'])
);
// save it to a file by serialiing
$filename = uniqid().'.text'; // you may play around with file names, rewrite this line so it will suit your convention - you may use counter of the files
file_put_content($filename, serialize($result));
}
To retrieve file and print it use something like this:
$file = $_GET['name'];
$file = str_replace(array('/', '.', '\'), '', $file); // for security reasons
$content = file_get_contents($file.'.text'); // note this is an example extension
$data = unserialize($content);
echo '<h1>'.$data['title'].'</h1>';
echo '<h2>'.$data['contenttitle'].'</h2>';
echo '<p>'.$data['content'].'</p>';
Related
Basically, I want to update static HTML files with code snippets input by users from a standard form. I understand how updating the files work, I'm just unsure as to how I go about including the code input from the form to my php file, which is shown below.
<?php
if($handle = opendir()) {
$search = '</body>';
replace = <<< EOF
<!-- I want to populate this with form field input.-->
EOF;
while(false !== ($entry = readdir($handle))) {
if(is_dir($entry)) continue;
$content = file_get_contents($entry);
$content = str_replace($search, $replace . '</body>', $content);
file_put_contents($entry, $content);
}
}
echo 'done';
?>
Any help greatly appreciated.
I would approach this a bit differently. I suggest having a template file aside from the one being modified. That way, you are always modifying a fresh copy instead of having to worry about what changed in the new version.
If your needs become more advanced beyond simply dropping in some markup, I might suggest using a DOM parser.
Finally, I'm sure you have a good reason for writing these static files... just remember the security implications of doing so. You're effectively letting someone do almost anything they want to your server.
Although I agreed it was in no way the best solution to the specific problem, for anyone that may find this useful in the future I used file_get and str_replace to achieve desired results. The code below will allow you to search a file for a specific term and replace with whatever you want based on form input.
<?php
//These are the variables the html file will post to the script.
$filename = $_POST['myFile'];;
$tofind = $_POST['myFind'];;
$toreplace = $_POST['myReplace'];;
$file = file_get_contents($filename);
$end = str_replace($tofind, $toreplace, $file);
$fp = fopen($filename, "w"); //Open the filename and set the mode to Write
if(fwrite($fp, $end)) ; //Write the New data to the opened file
fclose($fp); //Close the File
echo(" File name is $filename ... Finding $tofind .... Replacing with $toreplace ..... Done !");
?>
Though it's a horrible solution, mixed with your code this will do
$replace = array_key_exists('input',$_REQUEST) ? $_REQUEST['input'] : '';
//$replace = sanitize($replace); // there's so much bad with this string
//do the needfull
?>
<form method='GET' action='#'>
<input type='text' name='input' />
<input type='submit' />
</form>
I am creating a slideshow editor. I have been able to parse a file and present it to the user in a form. Now I need to figure out how to write the saved information to the file. I want the user to be able to edit the information before and after the slideshow, so there is no specific set of information to be able to overwrite the whole file.
If there is a way to get all of the text before the div and copy it to the variable, add the new information, then get the rest of the information after the div and add that to the variable and then write all that information to the file, then that would work. Otherwise, here is what I have put together.
/* Set Variables */
$x = $_POST['x'];
$file = $_POST['file'];
$path = '../../yardworks/content_pages/' . $file;
$z=0;
while ($z<$x){
$title[$z] = $_POST['image-title'.$z];
$description[$z] = $_POST['image-desc'.$z];
$z++;
}
for ($y=0; $y<$x; $y++){
$contents .= '<li>
<a class="thumb" href="images/garages/'.$file[$y].'">
<img src="images/garages/'.$file[$y].'" alt="'.$title[$y].'" height="100px" width="130px" class="slideshow-img" />
</a>
<div class="caption">
<div class="image-title">'.$file[$y].'</div>
<div class="image-desc">'.$description[$y].'</div>
</div>
</li>';
}
/* Create string of contents */
$mydoc = new DOMDocument('1.0', 'UTF-8');
$mydoc->loadHTMLFile($path);
$mydoc->getElementById("replace")->nodeValue = $contents;
$mydoc->saveHTMLFile($path);
$file = file_get_contents($path);
$file = str_replace("<", "<", $file);
$file = str_replace(">", ">", $file);
file_put_contents($path, $file);
?>
Nothing throws out an error, but the file also remains unchanged. Is there anything I can change or fix to make it write to the file? This is all I have been able to find regarding this specific problem.
I would like to stick to one language, but if I find a way to write to the file using javascript, do the php variables pass on to the javascript section or do I have to stick with one language?
**Edit
Everything is working. ONE problem: is there a way to keep the special characters without converting them? I need the < and > to stay as they are and not convert to a string
I have decided to save the file as it is and use a separate code set to replace the string. I have edited my question above.
I need to display the content of doc file inside CKeditor.
I read the content of doc file & passing it into an array line by line :
$rs = fopen("text.doc", "r");
while ($line = fgets($rs, 1024)) {
$this->data[] = $line . "<BR>";
}
then I create an instance of CKeditor:
include_once("ckeditor/ckeditor.php");
$CKeditor = new CKeditor();
$CKeditor->basePath = '/ckeditor/';
foreach ($this->data as $value) {
//what should I write here
}
$CKeditor->editor('editor1');
the CKeditor work right now & appear on my webpage .. but without any content ?
what should I right inside the foreach to passing array content into the editor ?
please help =(
.doc files are zipped up and cannot be read like this, by line. Consider using PHPWord to get access to the contents inside.
EDIT: Looks like PHPDoc can only write and not read, upon further investigation.
PHP tools are very deficient in this area. Your best bet is to use something like DocVert to do your file conversions on the command line. THEN you could load that document inside CKEditor.
EDIT: after OP's comment:
let's consider it's a txt file ... I need the Ckeditor method
Load your decoded HTML content into a Textarea, and give this textarea an HTML ID or class:
$textarea_content = htmlspecialchars_decode(file_get_contents('text.doc'));
Then, in your HTML, call the CKEditor inside a JavaScript tag to replace the textarea with the editor:
<html>
<head>
<!-- include CKEditor in a <script> tag first -->
<script type="text/javascript">
window.onload = function()
{
CKEDITOR.replace( 'editor1' );
};
</script>
</head>
<body>
<textarea id="editor1" name="editor1"><?php echo $textarea_content ?></textarea>
</body>
The documentation page has a lot more details.
I have a variable which calls content from database, sample is below
$content = '<div><h1>content here</h1>
<img src = 'image.jpg' /><br />
[code]echo 'welcome';[/code]
<h2>some content here</h2>
<p> some large content here</p>
[code]echo 'Click Here';[/code]
Thank you.';
echo 'headers here' .$content . 'footers here';
how can i execute PHP for the content in between [code] and [/code] tags?
remaining text would written as html execpt the codes used in [code] some php code [/code] tags
If this is for a templateting type of system for your site I would suggest to go a different route.
First your above code would change so that the portion between you code would look like:
[code]{{msg}}}[/code]
Now, you can still store your stuff in the database as php code if you want, but it makes more sense to eval it before you put it into the database, but I strongly suggest otherwise and go with a find/replace system.
Now, you would want to have a function that would do the following:
function output_template( $name, $data ) {
$template_string = get_template_from_db( $name );
for( $data as $k )
{
$template_string = str_replace( $k, $data[$k], $template_string );
}
return $template_string;
}
Then you can echo out the return value of this function. If you think you need to use eval, rethink what you are doing, especially for what appears to be a templating system.
I have the following PHP code which is a file name I'm using as the title of my pages for each download I provide to viewers. I want to be able to have SEO-friendly titles and URLs.
<?php echo $file_info[21]; ?>
The URLs of each page look similar to this:
http://site.com/directory/download_interim_finder.php?it=ZWtkaW1rcmInbn1ma3h9aXFLcHN6fWVzeDgnISJoO3tran0reW97cSd1YHR5YHZIbWBmfzxwY3tiOnxycnA8Y25zfWpna3Y+YXtlZnl7dnJLf3J8cWd2fT4kJCIramFbcm1tbWRqdHJmJnR2ZGhKd3JpdShQdGV+bX09fWh0b2JnfXx9anV2J3F7YHh4d3Z2S3Jyf3ZmdX8pNyg1anZ/bnhpdGJwVnx5cXF5Zn5bYW14cHp2bTQiKXFsYw==
I would like to convert this long URL to something a little more SEO-friendly and that will incorporate the php code I have specified above to something like "filename.php" instead of the long URL you see here.
I was hoping to use .htaccess but I've read that you can't do it through there. I've spent hours looking for ways to fix this and can't find a solution. Any help would be appreciated.
Thanks!
Step By Step instruction about create SEO friendly URL with dynamic content using PHP and .htaccess mod redirection. Friendly URLs improves your site search engines ranking. Before trying this you have to enable mod_rewrite.so module at httpd.conf. It’s simple just few lines of PHP code converting title data to clean URL format.
Database
Sample database blog table columns id, title, body and url.
CREATE TABLE `blog`
(
`id` INT PRIMARY KEY AUTO_INCREMENT,
`title` TEXT UNIQUE,
`body` TEXT,
`url` TEXT UNIQUE,
);
Publish.php
Contains PHP code. Converting title text to friendly url formate and storing into blog table.
<?php
include('db.php');
function string_limit_words($string, $word_limit)
{
$words = explode(' ', $string);
return implode(' ', array_slice($words, 0, $word_limit));
}
if($_SERVER["REQUEST_METHOD"] == "POST")
{
$title = mysql_real_escape_string($_POST['title']);
$body = mysql_real_escape_string($_POST['body']);
$title = htmlentities($title);
$body = htmlentities($body);
$date = date("Y/m/d");
//Title to friendly URL conversion
$newtitle = string_limit_words($title, 6); // First 6 words
$urltitle = preg_replace('/[^a-z0-9]/i', ' ', $newtitle);
$newurltitle = str_replace(" ", "-", $newtitle);
$url = $date . '/' . $newurltitle . '.html'; // Final URL
//Inserting values into my_blog table
mysql_query("insert into blog(title,body,url) values('$title','$body','$url')");
}
?>
<!--HTML Part-->
<form method="post" action="">
Title:
<input type="text" name="title"/>
Body:
<textarea name="body"></textarea>
<input type="submit" value=" Publish "/>
</form>
Article.php
Contains HTML and PHP code. Displaying content from blog table.
<?php
include('db.php');
if($_GET['url'])
{
$url =mysql_real_escape_string($_GET['url']);
$url = $url . '.html'; //Friendly URL
$sql = mysql_query("select title,body from blog where url='$url'");
$count = mysql_num_rows($sql);
$row = mysql_fetch_array($sql);
$title = $row['title'];
$body = $row['body'];
}
else
{
echo '404 Page.';
}
?>
<!-- HTML Part -->
<body>
<?php
if($count)
{
echo "<h1>$title</h1><div class='body'>$body</div>";
}
else
{
echo "<h1>404 Page.</h1>";
}
?>
</body>
.htaccess
URL rewriting file. Redirecting original URL 9lessons.info/article.php?url=test.html to 9lessons.info/test.html
RewriteEngine On
RewriteRule ^([a-zA-Z0-9-/]+).html$ article.php?url=$1
RewriteRule ^([a-zA-Z0-9-/]+).html/$ article.php?url=$1
When I look at the URL example you posted I see two things:
It points to a PHP script:
/directory/download_interim_finder.php
It has a (looks like a) Base64 encoded argument which obviously no human can read
I'm guessing your input for the download script is encoded in it-argument string, you will need to put this in the URL else you cant identify which file you want to download. Since you want your URLs to be semantic you have to add your semantic information to the URL.
You could for instance make URLs like:
/download/documentation/productX/[[Base64encodedinputstring]]
In your .htaccess you then need the rule:
RewriteEngine On
RewriteRule /download/.*/([^\/]+) /directory/download_interim_finder.php?it=$2
If your host does not allow you to use .htaccess then you can not easily imploy semantic urls.