- Joined
- Apr 29, 2005
- Messages
- 6,407
- Reaction score
- 130
So this is my first try at OOP and I think I did a pretty good job, however the only thing I did was read a tutorial so I really don't have clue if I even understood the concept of classes properly, so some suggestions would be nice. Should I use more functions? More classes? Write less terrible regexes? Put on a silly hat and jump out of a window?
What does it do?
The script reads html tags from a source which have as title "pie.something" and then displays the content of those tags in a form. You can even add indicators, :text: means that the content of the tags needs to be displayed in a text area and :nocont: indicates the beginning of editable content and should be wrapped around the tags which have "pie.something" as title, this tag is mandatory.
Then if the content is edited it can be written back to the html file.
Example
The file where content gets written to is located here (Template from freetemplates):
And the actual script in action is located here:
Look at the source and you'll get how the script works.
The script
I tried to keep the commenting to a bare minimum and as I mentioned earlier on please tell me if there's something I can improve on.
What does it do?
The script reads html tags from a source which have as title "pie.something" and then displays the content of those tags in a form. You can even add indicators, :text: means that the content of the tags needs to be displayed in a text area and :nocont: indicates the beginning of editable content and should be wrapped around the tags which have "pie.something" as title, this tag is mandatory.
Then if the content is edited it can be written back to the html file.
Example
The file where content gets written to is located here (Template from freetemplates):
You must be registered to see links
And the actual script in action is located here:
You must be registered to see links
Look at the source and you'll get how the script works.
The script
I tried to keep the commenting to a bare minimum and as I mentioned earlier on please tell me if there's something I can improve on.
PHP:
<?php
//This class generates the form.
class createform {
public $index;
protected function getfile() {
if(($file = @file_get_contents($this->index)) == false) {
die("Failed to open index file.");
}
return $file;
}
//This function singles out the tags that have "pie.something" as title.
protected function get_pie_as_title() {
$file = $this->getfile();
//This regex matches all tags wrapped in the <div title=pie.someting:text:> tag
preg_match("/\<div(.*?)title\=\\\"(.*?):nocont\:\\\"(.*?)\>(.*?)\<\/div\>/is", $file, $content);
//The above regex isn't able to not-match the :nocont: tag therefor it needs to be stripped away.
$content = preg_replace("/\<div(.*?)title\=\\\"(.*?):nocont:\\\"(.*?)\>/is", "", $content[0]);
//This regex singles out the tags that have "pie.something" as title and puts them in an array.
preg_match_all("/\<(.*?)title\=\\\"pie.(.*?)\\\"(.*?)>(.*?)\<\/(.*?)\>/is", $content, $matches);
return $matches;
return $file;
}
//This function generates the actual form.
public function create_form() {
$matches = $this -> get_pie_as_title($file);
echo "<form action='".$_SERVER['PHP_SELF']."' method='post'>";
$i = 0;
while($i < count($matches[2])) {
echo "".ucfirst(str_replace(":text:", "", $matches[2][$i])).":";
if(strlen($matches[4][$i]) > 50 || preg_match("/:text:/", $matches[2][$i])) {
$formname = $matches[2][$i];
$formtext = $matches[4][$i];
include("editor.php");
} else if(!preg_match("/:nocont:/", $matches[2][$i])) {
echo "<br/><input type ='text' name='".$matches[2][$i]."' value='".$matches[4][$i]."';/><br/>";
}
$i ++;
}
echo "<input type='submit' name='submitentry' value='Submit new entry'>";
echo "</form>";
}
}
//This child class writes the new content to the html file.
class submitcontent extends createform {
//This function pieces the seperate tags with their new content together.
protected function create_new_cont() {
$matches = $this -> get_pie_as_title();
$file = $this -> getfile();
$i = 0;
while($i < count($matches[2])) {
$form[$matches[2][$i]] = $_POST[$matches[2][$i]];
//This regex singles out the tag that has $matches[2][$i] as title.
$spattern = sprintf('#<([a-z0-9]+)[^>]title=\"%s\"[^<]*>.*</\\1>#','pie.'.$matches[2][$i].'');
preg_match($spattern, $file, $tag);
//'Cause I know atleast one of you bastards is going to attempt XSS.
$newtag = preg_replace("/\>.*\</s", ">".htmlentities($form[$matches[2][$i]])."<", $tag[0]);
$newcontent = "".$newcontent."
".$newtag."";
$i ++;
}
return $newcontent;
}
public function write_to_file() {
$file = $this -> getfile();
$newcontent = $this -> create_new_cont();
preg_match("/\<div(.*?)title\=\\\"(.*?):nocont\:\\\"(.*?)\>/is", $file, $main);
$newcontent = "".$main[0]."".$newcontent."";
//This variable contains the entire source with the new content that's going to be written to the file.
$file = preg_replace("/\<div(.*?)title\=\\\"(.*?):nocont\:\\\"(.*?)\>(.*?)\<\/div\>/is", $newcontent, $file);
if(file_put_contents($this->index, stripslashes($file)) == false) {
die("Failed to write to index file.");
} else {
header("Location: /pielite/");
}
}
}
if(!isset($_POST['submitentry'])) {
$form = new createform;
$form -> index = "index.htm";
$form -> create_form();
} else {
$scont = new submitcontent;
$scont -> index = "index.htm";
$scont -> write_to_file();
}
?>