sNews Forum

Previous sNews versions => sNews 1.4 Final => Engine development => Topic started by: bryn on January 21, 2006, 08:02:52 pm

Title: sNews 1.4 code amendments and snippets
Post by: bryn on January 21, 2006, 08:02:52 pm
I've started a new thread here so that we can keep all the code amendments and snippets offered here in the forum, in one place..hopefully this will make it easier for everyone to track changes as and when they occur.

Please put all your new tested code here..many thanks ;D

Here'e the latest code offered by Luka for sNews v1.4 as of Friday 20th January GMT

http://www.solucija.com/snews/testing/snews.txt

And Alberts version with line numbers

http://snews.awddesign.co.uk/snews/snews14code/snews.php

The latest sql text seems to be as follows

Code: [Select]
CREATE TABLE articles (
  id INT PRIMARY KEY AUTO_INCREMENT,
  title VARCHAR(100) DEFAULT NULL,
  seftitle VARCHAR(100) DEFAULT NULL,
  text LONGTEXT,
  textlimit INT(5) NOT NULL DEFAULT '0',
  date DATETIME DEFAULT NULL,
  category INT(8) NOT NULL DEFAULT '0',
  position CHAR(3),
  displaytitle CHAR(3) NOT NULL DEFAULT 'YES',
  displayinfo CHAR(3) NOT NULL DEFAULT 'YES',
  commentable VARCHAR(5) NOT NULL,
  image varchar(30) DEFAULT NULL,
  published INT(3) NOT NULL DEFAULT '1'

);


CREATE TABLE categories (
  id int(8) PRIMARY KEY AUTO_INCREMENT,
  name varchar(40) NOT NULL,
  seftitle VARCHAR(100) DEFAULT NULL,
  description varchar(100) NOT NULL,
  published varchar(4) NOT NULL DEFAULT 'YES'
);

CREATE TABLE comments (
  id INT(11) PRIMARY KEY AUTO_INCREMENT,
  articleid INT(11) DEFAULT '0',
  name varchar(50) DEFAULT '',
  comment TEXT,
  time DATETIME DEFAULT NULL
);

Please report on any problems with the code snippets in this thread ;D

Title: sNews 1.4 code amendments and snippets
Post by: bryn on January 22, 2006, 03:39:47 pm
added by analyzerx on 21 Jan 2006

.htaccess

I've noticed some problems with URL rewrites so I've changed it a bit for anyone who has the same problems with me...

.htaccess did not check if the file I asked in the URL actually existed so I could not put anything else in the same folder as snews... (I could not access even my images folder for example...)

so this is my new .htaccess

Code: [Select]
#sNews v1.4
#php_flag zlib.output_compression On
#php_value zlib.output_compression_level 5


php_value session.use_trans_sid 0

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule ^(.*)/$ index.php?category=$1 [L,QSA]
RewriteRule ^([a-z_]+)/([^/]+)/ index.php?category=$1&title=$2 [L,QSA]
RewriteRule ^([a-z_]+)/([^/]+)/([^/]+)/ index.php?category=$1&title=$2&commentspage=$3 [L,QSA]

I've added compression but you'll need to uncomment the 2nd and 3rd line...
From there before using rewrites apache will check if the directory/file actually exists...

for example if you have a 1.4 installation try accessing your image folder...
most likely you'll get a snews error...
after changing the .htaccess you will be able to access it... ^_^

Contact form bugfix

also this is a quick fix for the contact form in snews.php 1.4 testing2/3...
find:

Code: [Select]
// CONTACT
function contact() {
if ($_POST['contactform'] == "") {?>



:





:





:





:

 


     


  if (isset($_POST['contactform'])) {
$to = s('website_email');
$subject = s('contact_subject');
$body = l('name') .": ". $_POST['name'] ."n";
$body .= l('email') .": ". $_POST['email'] ."n";
$body .= l('url') .": ". $_POST['weblink'] ."nn";
$body .= l('message') .": ". $_POST['message'] ."n";
if (strlen(clean($_POST['name'])) > 1 AND strlen(clean($_POST['message'])) > 1) {
mail($to, $subject, $body);
echo "

". l('contact_sent') ."

";
  echo "

". l('backhome') ."

";
} else {
echo "

". l('contact_not_sent') ."

";
echo "

". l('message_error') ."

";
echo "

". l('backhome') ."

";
}}}

and replace it with:

Code: [Select]
// CONTACT
function contact() {
if ($_POST['contactform'] == "") {?>



:




:




:




:


 


     




  if (isset($_POST['contactform'])) {
$to = s('website_email');
$subject = s('contact_subject');
$body = l('name') .": ". $_POST['name'] ."n";
$body .= l('email') .": ". $_POST['email'] ."n";
$body .= l('url') .": ". $_POST['weblink'] ."nn";
$body .= l('message') .": ". $_POST['message'] ."n";
if (strlen(clean($_POST['name'])) > 1 AND strlen(clean($_POST['message'])) > 1) {
if ($contact_mail_just_sent != 1) {
mail($to, $subject, $body);
$contact_mail_just_sent = 1;
echo "

". l('contact_sent') ."

";
  echo "

". l('backhome') ."

";
};
} else {
echo "

". l('contact_not_sent') ."

";
echo "

". l('message_error') ."

";
echo "

". l('backhome') ."

";
}}}

^_^ :)
Title: sNews 1.4 code amendments and snippets
Post by: George Antoniadis on January 24, 2006, 06:24:00 pm
This is a basic RSS Feed script until 1.4 is release to the world! ^_^
just place it in the same directory with snews.php
Code: [Select]
header('Content-type: text/xml');
include 'snews.php';

echo '


'.s('website_title').'
'.s('website_title').'
'.s('website').'
Copyright '.s('website_title').'';

$db = mysql_connect(s(dbhost),s(dbuname),s(dbpass));
mysql_select_db(s(dbname)) or die(s(dberror));
$query = "SELECT * FROM " .s('prefix'). "articles WHERE position = 1 ORDER BY date DESC LIMIT ".s('rss_limit');
$result = mysql_query($query);

while ($r = mysql_fetch_assoc($result)){
$in_cat_name = '';
$in_cat_seftitle = '';

if ($r['category'] != 0) {
$cat_id_temp = $r['category'];
$cat_query = "SELECT * FROM " .s('prefix'). "categories WHERE id = $cat_id_temp";
$cat_result = mysql_query($cat_query);
while ($c = mysql_fetch_array($cat_result)) {
$in_cat_seftitle = $c['seftitle'];
$in_cat_name = $c['name'];
}
} else
{
$in_cat_seftitle = s('home');
$in_cat_name = s('home');
};

$pattern="'<[/!]*?[^<>]*?>'si";
$replace="";
$description = preg_replace($pattern, $replace, stripslashes($r['text']));

echo "";
echo  "". $r['title'] . " (posted in ". $in_cat_name .")";
echo  "". nl2br(stripslashes($r['text'])) ."";
echo  "". s(website) ."".$in_cat_seftitle."/". $r['seftitle'] ."/";
echo  "
";
};


echo '

';

?>

PLEASE BE SURE NOT TO LEAVE ANY BLANK SPACES, LINES OR ANYTHING BEFORE THE FIRST
Title: sNews 1.4 code amendments and snippets
Post by: bryn on January 24, 2006, 06:46:59 pm
How to remove the category shown in New Articles:

Posted by Luka - 24th Jan 2006

Code: [Select]
// NEW ARTICLES
function new_articles($number) {
$db = mysql_connect(s('dbhost'),s('dbuname'),s('dbpass'));
mysql_select_db(s('dbname')) or die(s('dberror'));
$query = "SELECT * FROM " .s('prefix'). "articles WHERE position = 1 ORDER BY id DESC LIMIT 0, $number";
$result = mysql_query($query);
while ($r = mysql_fetch_array($result)) {
$date = date(s('date_format'), strtotime($r['date']));
echo "

$r[title] ";
echo $date;
echo "

"; }
}
Title: sNews 1.4 code amendments and snippets
Post by: Jochum Meester on January 26, 2006, 11:29:26 pm
Could you please also add a default index.php file with all the required functions (like etc., because it's doing nothing for me now... I've copied the 1.3 index.php to this new snewsversion, but it doesn't seem to know the past entries function, and whenever I click a post's title it messes up with CSS, because the path to the CSS is changed because it uses those friendly urls.
Title: sNews 1.4 code amendments and snippets
Post by: George Antoniadis on January 27, 2006, 12:44:32 am
Quote from: JM
Could you please also add a default index.php file with all the required functions (like etc., because it's doing nothing for me now... I've copied the 1.3 index.php to this new snewsversion, but it doesn't seem to know the past entries function, and whenever I click a post's title it messes up with CSS, because the path to the CSS is changed because it uses those friendly urls.

index has not changed... just remember you NEED to use cause without it nothing else will happen
Title: sNews 1.4 code amendments and snippets
Post by: Jochum Meester on January 27, 2006, 09:04:30 am
http://www.iseeyou-designs.com/projects/snewstest/
Stops at past_entries

Perhaps we need another similir topic for problems, to avoid getting all the questions in this one?
Title: sNews 1.4 code amendments and snippets
Post by: Luka on January 27, 2006, 12:32:38 pm
Quote from: JM
http://www.iseeyou-designs.com/projects/snewstest/
Stops at past_entries

Perhaps we need another similir topic for problems, to avoid getting all the questions in this one?

Change that to past_articles(3,3);

New Articles and Past Articles. I changed that so we don't confuse entries and articles since Articles is used throught the site and administration.
Title: sNews 1.4 code amendments and snippets
Post by: Patric Ahlqvist on January 27, 2006, 12:35:30 pm
EDIT --> Neva' mind ;)
Title: sNews 1.4 code amendments and snippets
Post by: albert on February 07, 2006, 04:36:19 pm
Hi

does any one know how to make the email save to a text file.

ie i would like to turn off mail..

Code: [Select]
// mail($to, $subject, $body, $headers);
and then have a txt file to pick up what was posted??
any help would be apeciated.

Albert
Title: sNews 1.4 code amendments and snippets
Post by: Rehuel Lobato de Mesquita on February 07, 2006, 04:46:08 pm
Code: [Select]
$mailbody = "To: ".$to."rn";
$mailbody .= "Subject: ".$subject."rn";
$mailbody .= "Header: ".$headers."rn";
$mailbody .= "Message: ".$message;
$filename = "Mail-from-".$to.".txt";
$fp = fopen($filename, "w");
if (!$fp) die ("cannot open the file");
fputs($fp, $mailbody, 4096);
fclose($fp);
?>

This should do it, I guess
Title: sNews 1.4 code amendments and snippets
Post by: Mario on February 07, 2006, 05:01:55 pm
Quote from: Stretsh
Code: [Select]
$mailbody = "To: ".$to."rn";
$mailbody .= "Subject: ".$subject."rn";
$mailbody .= "Header: ".$headers."rn";
$mailbody .= "Message: ".$message;
$filename = "Mail-from-".$to.".txt";
$fp = fopen($filename, "w");
if (!$fp) die ("cannot open the file");
fputs($fp, $mailbody, 4096);
fclose($fp);
?>

This should do it, I guess

Nice!

Just a thought. Instead of writing it to a plain txt file writing it to a xml/rss file would be nice (with the right syntax of course).  This way we could check incoming messages automaticly with a rss feed reader. Opera has a feed reader built in - not sure about other browsers.
Title: sNews 1.4 code amendments and snippets
Post by: Luka on February 07, 2006, 05:05:07 pm
Quote from: Mario

This way we could check incoming messages automaticly with a rss feed reader.


Yeah, and so could anyone else. It would be wise to store the xml file to some wierd folder and name it 2349608.xml  ;D
Title: sNews 1.4 code amendments and snippets
Post by: Mario on February 07, 2006, 05:12:38 pm
I Agree!

Something like this would take care of that:

Code: [Select]
$s['myprivatefeed'] = "myprivatefeed.xml; // make sure to change this to a hard to guess url; eg 123Q1aa.xml
Title: sNews 1.4 code amendments and snippets
Post by: albert on February 08, 2006, 03:55:35 am
Hi

thank you for the replay's to the email save to text file.

i did change this
Code: [Select]
$fp = fopen($filename, "w");to this
Code: [Select]
$fp = fopen($filename, "a");
and works great. Big thank you :)

Now with the problem with people reading my file, what a bumer. So the best way would be to incert this into
the data base.


Code: [Select]
mail("spam@gmail.com","Web Site email from:web site","From: ".$Name."<".$Emailaddress.">");

$link = mysql_connect("localhost","root","passmyspam");

mysql_select_db("snews",$link);
$query="insert into mail (Name,Email,Weblink,Message) values ('".$Name."','".$Email."','".$Weblink."','".$Message."')";
mysql_query($query);

i think this may work, What do ya think ?

Albert
Title: sNews 1.4 code amendments and snippets
Post by: Rehuel Lobato de Mesquita on February 08, 2006, 04:18:55 pm
no, this won't work

You have to create another table in your snews database, call it mail or something, with the fields id, from, email, link, body. (id should be primary, auto_increment)

Then your script should look like this:

Code: [Select]
$link = mysql_connect("localhost","root","passmyspam");

mysql_select_db("snews",$link);
$query="insert into mail (from, email, link, body) values ('".$Name."','".$Email."','".$Weblink."','".$Message."')";
mysql_query($query);

Then, for ease of use, you can make a separate html file to lookup your mail
Title: sNews 1.4 code amendments and snippets
Post by: Mario on February 08, 2006, 04:49:33 pm
Quote from: albert

Now with the problem with people reading my file, what a bumer. So the best way would be to incert this into
the data base.

Maybe by writing the file to a subdirectory and protect that dir with .htaccess ?
Title: sNews 1.4 code amendments and snippets
Post by: Jochum Meester on February 17, 2006, 10:51:39 pm
Quote from: Stretsh
Code: [Select]
$mailbody = "To: ".$to."rn";
$mailbody .= "Subject: ".$subject."rn";
$mailbody .= "Header: ".$headers."rn";
$mailbody .= "Message: ".$message;
$filename = "Mail-from-".$to.".txt";
$fp = fopen($filename, "w");
if (!$fp) die ("cannot open the file");
fputs($fp, $mailbody, 4096);
fclose($fp);
?>
Where do I need to put the above?
And this?

Code: [Select]
$fp = fopen($filename, "a");
if (!$fp) die ("cannot open the file");
fputs($fp, $mailbody, 4096);
fclose($fp);
Title: sNews 1.4 code amendments and snippets
Post by: bryn on February 22, 2006, 07:30:45 pm
hey guys..nice work on the code..but I really wanted to try and keep this thread clean so that we have a central repository for tested code and snippets ;)

it makes it so much easier for users to "find that piece of code that did this!"..instead of having to search through the forum or listings..which can be very frustrating as I'm sure you know. topic creep is just murder to keep track of  :'(

I will leave this thread as is for a week, and then attempt to clean and move the contents so we're back straight again ok..for the good of the users..thanks guys ;D
Title: sNews 1.4 code amendments and snippets
Post by: Patric Ahlqvist on February 28, 2006, 07:32:43 am
As you're deleting "s*itty" posts bryn, I'm gonna answer this guy, ok.

I don't know WP at all...Do you want a styleswitcher or do you wanna show more templates ? For the first contact Albert, he's got a nice styleswitcher, för the second you just create new folders and upload snews files too, if you want different contentc on each instal, create a new set of tables with a prefix, and your set.

P
Title: sNews 1.4 code amendments and snippets
Post by: albert on February 28, 2006, 02:14:16 pm
Hi

Wordpress?
Hang tight Wordpress designs will work, you just need to strip the code down.
 
As Wordpress has a lot of GPL designs will take a look soon :)

Albert
Title: sNews 1.4 code amendments and snippets
Post by: albert on February 28, 2006, 02:40:40 pm
If you put snews to a second level of any directory / root you will need to tell .htaccess

                                           |---- That is the second level
                                           |
http://WWW.MYSITE.com/snews/index.php

                                                       
Code: [Select]
main directory / root
|
|index.php
|
|
|  snews.php  -- version of v1.4
|
| .htaccess v1.4
|
|  rss.xml
|
|  readme.txt              
|                      
|-------/images  
|
|-------/img cmd 777
|
|-------/mods
|
|


#  .htaccess
#  sNews v1.4  16-feb-2006
#  Copyright(c) 2006, Solucija - All rights reserved

#  Uncomment following line if your webserver's URL
#  is not directly related to physival file paths.
#  Update Your Directory (just / for root)

RewriteBase /snews


Add this snipit to the sNews 1.4 readme.txt file by Albert :) It will help us All.

Albert
Title: sNews 1.4 code amendments and snippets
Post by: bryn on February 28, 2006, 09:07:42 pm
 ;D ;D ;D

ok..looks like my idea has fallen by the wayside..maybe a different method of thread ;)
Title: sNews 1.4 code amendments and snippets
Post by: paul on May 01, 2006, 09:12:18 pm
After tearing my hair out for a while, wondering why some forms work and some don't, I eventually found out the default search form doesn't terminate.

Anyway, the fix was simply to terminate the form tag in snews.php:

Line 923
Code: [Select]
// SEARCH FORM
function searchform() { ?>


 



Title: sNews 1.4 code amendments and snippets
Post by: Patric Ahlqvist on May 03, 2006, 08:56:49 am
well, it does kind of end:
Code: [Select]
   
with that trailing slash in it...But that do some things to other applications like JS insertions in some scripts wont work. So I have done the same ;)