SimplePortal

Customization => Custom Coding => Topic started by: NemWar on May 07, 2011, 12:39:37 PM

Title: PHP-Box errors with "Link It!" Script
Post by: NemWar on May 07, 2011, 12:39:37 PM
Hello,

sorry if my english is not the best at all, 'cause i'm from germany and don't use english often.

Now my little problem:

I want to use an PHP-Code called LinkIt!.
It generates an WebLink-list where users can add their links.
These links are stored in the mysql-database.

Well, the preview works but if i want to store this block i get an error in the php-code displayed.

Also i have to say, i can't handle php-codes so maybe anyone want to help me?

Otherwise, witch solution are you using for store web-links for you're users?

Here's the code, maybe you wanna risk a look:

Code: [Select]
//////////////////////////////////////////////
// Linkit Version 1.7.1
// Developed by Thurnok
// thurnok -AT- tinyport .DOT. net
// Complete Computer Services
// August 22, 2006
//
// Last update May 18, 2007
//
// 1.7.1:
//      - added explicit sort by ID when $li_sort = 0 so that if you edit your database table, changing ID numbers,
//         you can sort by the ID instead of the order data was entered into the table
//
// This is a php block and/or php Article snippet.  It works in any block
// position (left/right/center/frontpage/article).
// It allows for multiple columns of links via a table.
//
// Linkit allows you to give your users the ability to add links to your
// site in a Tiny Portal block that you designate.  Your Admins can
// edit/remove entries, and you determine what 'groups' can post links!
// You can also give groups abilities to edit or delete links as well.
// Additionally, you can allow members to edit/delete their own submissions.
//
// This script will create a table for the links using your current
// TP table prefix followed by linkit (ex: smf_tp_linkit) and using your
// current database credential information.
// NOTE: Your database user/permissions used for SMF must allow you to
// create a table or you will never be able to store the links.  You
// can create the table manually if necessary.
//
//////////////////////////////////////////////

/*
****************************************
****************************************
*** !! User Configuration Section !! ***
****************************************
****************************************
*/
// *****   SECURITY OPTIONS   *****
// who has addlink access? -  format is array('<groupnum>', '<groupnum>', ...)
// Example: $li_addlink_groups = array('14');
$li_addlink_groups = array('');
// who can edit links? - same format as addlink
$li_editlink_groups = array('');
// who can delete links? - same format as addlink
$li_dellink_groups = array('');
// group that you want members denied usage even if in a group above
$li_deny_groups = array('');
// members you want to deny usage even if allowed above
$li_deny_members = array('');
// allow members to edit/delete their own links? (0 = No, 1 = Yes)
$li_edit_own = 0;
$li_del_own = 0;

// *****   LAYOUT OPTIONS   *****
// size of the edit boxes (<input>)
$li_editbox_size = 15;
// max size of the input for the URL Name (30 or less is best)
$li_urlname_maxsize = 30;
// sorting option - 0 = chronological, 1 = alphabetic, 2 = submitter
$li_sort = 0;
// sorting direction - 0 = ascending, 1 = descending
$li_sort_direction = 0;
// number of columns of links to display
$li_columns = 1;
// want to add your own styles to the rows/columns?  change the tag info here
$li_start_row = '<tr>';
$li_start_col = '<td>';
// approximate number of lines displayed in block 0 = unlimited
$li_blocklines = 20;

// you can make the columns use one of TinyPortal's text classes ("normaltext" or "smalltext") or none.
// this will add to the $li_start_col variable the classtype you choose (0 = no class, 1 = smalltext, 2 = normaltext)
// if you are making your own text styles in the $li_start_col variable above, you should set $li_useclass = 0
$li_useclass = 1;

// make it easier to use non-blanking spaces (TP blocks/articles will remove them from your code the next time you edit the same block/article)
// using a variable instead, will let you keep that code intact.
$nbsp = '&'.'nbsp;';

// *****   OTHER OPTIONS   *****
// you can set the tablename to other than linkit if you like
$li_tablename = "linkit";
// and the title displayed
$li_title = "Linkit!";
// add some descriptive text here if you like to display under title
$li_desc = "Member added links! ".$nbsp."Please add only your most favorite links.";
// Print our title - or comment the line to not display a title
if (!empty($li_title))
   echo '<center><b>' . $li_title . '</b></center><br />';
if (!empty($li_desc))
   echo '<font size=1>' . $li_desc . '</font><p />';
/*
****************************************
****************************************
*/

//////////////////////////////////////////////
//
// The rest of this you should leave as is
// unless you are overly industrious :)
//
//////////////////////////////////////////////
// globals for database vars
global $db_prefix, $tp_prefix;
// globals for user information
global $context, $user_info, $ID_MEMBER;

// fix for TP 0.8.6 and lower
if (empty($tp_prefix)){
   $tp_prefix = $settings['tp_prefix'];
}

switch ($li_useclass){
   case 1:
      $li_classtxt = ' class="smalltext" ';
      $li_start_col = substr($li_start_col, 0, -1) . $li_classtxt . '>';
      break;
   case 2:
      $li_classtxt = ' class="normaltext" ';
      $li_start_col = substr($li_start_col, 0, -1) . $li_classtxt . '>';
      break;
   default:
      $li_classtxt = '';
      break;
}

// get our script url (including parameters - like ?page=6)
$myself = $_SERVER['REQUEST_URL'];

// put the SMF table prefix in front of your tablename from above
$li_tablename = $tp_prefix . $li_tablename;
// check if user is in a group that is allowed to add links
$li_add_auth = array_intersect($li_addlink_groups, $user_info['groups']);
// check if user is in a group that is allowed to edit links
$li_edit_auth = array_intersect($li_editlink_groups, $user_info['groups']);
// check if user is in a group that is allowed to delete links
$li_del_auth = array_intersect($li_dellink_groups, $user_info['groups']);
// deny if in one of the deny groups or members
if (array_intersect($li_deny_groups, $user_info['groups']) || @in_array($ID_MEMBER, $li_deny_members)){
   $li_add_auth = false;
   $li_edit_auth = false;
   $li_del_auth = false;
   $li_edit_own = false;
   $li_del_own = false;
}
// Admins are always allowed to add/edit/delete links
if ($context['user']['is_admin']){
   $li_add_auth = 1;
   $li_edit_auth = 1;
   $li_del_auth = 1;
}

// set up all our functions ahead of time
// function to create table if not already there
function LinkitCreateTable($li_tablename) {
   // set up the query that will create the table appropriately
   $dbquery   = "CREATE table $li_tablename (id INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
                        url_name TEXT, url TEXT, submitted_by TEXT);";
   if (!mysql_query($dbquery)) {
      die("Query Failed!  Table NOT Created!<br />\n");
   }
}

// function to add links to the table
function LinkitAddLink($li_tablename, $li_urlname, $li_urllink, $li_submittedby) {
   if ( (strtolower(substr($li_urllink, 0, 7)) != "http://") && (strtolower(substr($li_urllink, 0, 6)) != "ftp://") ){
      $li_urllink = "http://" . $li_urllink;
   }
   // first see if this would be a duplicate, if so, do not post it
   $dbquery = "SELECT * FROM $li_tablename
                     WHERE url LIKE '" . $li_urllink . "'";
   $dbresult = mysql_query($dbquery);
   if ($row = mysql_fetch_assoc($dbresult)){
      // if a row is found, then there's already this link in table, don't dupe it
      return;
   }
   $dbquery = "INSERT INTO $li_tablename VALUES (0, \"$li_urlname\", \"$li_urllink\", \"$li_submittedby\");";
   if (!mysql_query($dbquery)) {
      die("Query Failed!  Link NOT Inserted into database!<br />\n");
   }
}

// function to edit links in the table
function LinkitEditLink($li_tablename, $li_urlname, $li_urllink, $li_id) {
   // make sure only one ID is actually in there
   if ((string)(int)$li_id === (string)$li_id){
      // change only values that were there in form
      $dbquery = "UPDATE $li_tablename SET ";
      if ($li_urlname){
         // if there was a url name provided, add it to UPDATE query
         $dbquery .= "url_name=\"$li_urlname\" ";
      }
      if ($li_urllink){
         // add HTTP:// if necessary at front of link to prevent BASE URL applying in front of link provided
         if ( (strtolower(substr($li_urllink, 0, 7)) != "http://") && (strtolower(substr($li_urllink, 0, 6)) != "ftp://") ){
            $li_urllink = "http://" . $li_urllink;
         }
         // since url link was provided, add it to UPDATE query
         if ($li_urlname){
            // since we already added url name, put comma and space before the url link update part
            $dbquery .= ", url=\"$li_urllink\" ";
         } else {
            // didn't have an url name added, so no comma needed
            $dbquery .= "url=\"$li_urllink\" ";
         }
      }
      // add rest of query
      $dbquery .= "WHERE id=\"$li_id\";";
      if (!mysql_query($dbquery)) {
         die("Query Failed!  Link NOT modified in database!<br />\n");
      }
   }
}

// function to delete links from the table
function LinkitDelLink($li_tablename, $li_id) {
   // delete link(s) in $li_id
   $dbquery = "DELETE FROM $li_tablename WHERE id in ( " . $li_id . ")";
   if (!mysql_query($dbquery)) {
      die("Query Failed!  Link NOT Deleted from database!<br />\n");
   }
}

///////////  MAIN CODE HERE  ////////////
// convert $_POST vars to prevent undefined index errors
$li_add = empty($_POST['li_add']) ? 0 : 1;
$li_edit = empty($_POST['li_edit']) ? 0 : 1;
$li_del = empty($_POST['li_del']) ? 0 : 1;
$li_urlname = empty($_POST['li_urlname']) ? '' : $_POST['li_urlname'];
$li_urllink = empty($_POST['li_urllink']) ? '' : $_POST['li_urllink'];
$li_checklist = empty($_POST['li_checklist']) ? '' : $_POST['li_checklist'];
 
// if someone just added a link, post it to the database
if ($li_add){
   $li_urlname = trim($li_urlname);
   $li_urllink = trim($li_urllink);
   if ($li_urlname && $li_urllink){
      LinkitAddLink($li_tablename, $li_urlname, $li_urllink, $user_info['username']);
   }
}

// if someone just edited a link, modify it in database
if ($li_edit && $li_checklist){
   $li_urlname = trim($li_urlname);
   $li_urllink = trim($li_urllink);
   if ($li_urlname || $li_urllink){
      LinkitEditLink($li_tablename, $li_urlname, $li_urllink, $li_checklist);
   }
}

// if someone just deleted a link, remove it from database
if ($li_del && $li_checklist){
   LinkitDelLink($li_tablename, $li_checklist);
}

////////////  MAIN DISPLAY CODE HERE  ///////////////

// set query to select all data in appropriate order
switch ($li_sort){
   // alphabetical order
   case 1:
      $dbquery = $li_sort_direction ? "SELECT * from $li_tablename ORDER BY url_name DESC" : "SELECT * from $li_tablename ORDER BY url_name";
      break;
   // submitted by order
   case 2:
      $dbquery = $li_sort_direction ? "SELECT * from $li_tablename ORDER BY submitted_by DESC" : "SELECT * from $li_tablename ORDER BY submitted_by";
      break;
   // chronological order
   default:
      $dbquery = $li_sort_direction ? "SELECT * from $li_tablename ORDER BY id DESC" : "SELECT * from $li_tablename ORDER BY id ASC";
}

$dbresult = mysql_query($dbquery);
if (!$dbresult){
   if (mysql_errno() == 1146){
      // table doesn't exist, create it!
      LinkitCreateTable($li_tablename);
      // get our result again
      $dbresult = mysql_query($dbquery);
      if (!$dbresult) die("Unexpected error: " . mysql_error());
   } else {
      die("Unexpected error: " . mysql_error());
   }
}

// javascript validations
echo   '
<script type="text/javascript">
<!--
function addCheck(){
   urlname = document.li_form.li_urlname;
   urllink = document.li_form.li_urllink;
   if (urlname.value.replace(/ /g,"") == "" || urllink.value.replace(/ /g,"") == ""){
      alert("Missing information - Must supply both URL Name and URL Link!");
      return false;
   }
}

function editCheck(){
   retval = false;
   checklist = "";
   numchecked = 0;
   urlname = document.li_form.li_urlname;
   urllink = document.li_form.li_urllink;
   checkboxes = document.li_form.li_checkbox;
   if (urlname.value.replace(/ /g,"") != "" || urllink.value.replace(/ /g,"") != ""){
      for (i=0; i<checkboxes.length; i++){
         if (checkboxes[i].checked == true){
            checklist = checkboxes[i].value;
            numchecked++;
         }
      }
      switch (numchecked){
         case 0:
            alert("You must select a link to edit first!");
            break;
         case 1:
            document.li_form.li_checklist.value = checklist;
            retval = true;
            break;
         default:
            alert("You can only edit one link at a time!");
            break;
      }
   } else {
      alert("No information entered!");
   }
   return retval;
}

function delCheck(){
   retval = false;
   checklist = "";
   checkboxes = document.li_form.li_checkbox;
   for (i=0; i<checkboxes.length; i++){
      if (checkboxes[i].checked == true){
         checklist += (checklist != "" ? "," : "") + checkboxes[i].value;
      }
   }
   document.li_form.li_checklist.value = checklist;
   if (checklist != ""){
      retval = true;
   }
   if (!retval){
      alert("Select a link first!");
   }
   return retval;
}

function urlTest(){
   urllink = document.li_form.li_urllink.value;
   if (urllink.replace(/ /g,"") != ""){
      if (urllink.toLowerCase().substr(0, 7) != "http://" && urllink.toLowerCase().substr(0, 8) != "https://"){
         urllink = "http://" + urllink;
      }
      // display a new window and open url in it
      window.open(urllink, "TestUrl", "width=600px, height=400px, resizable, scrollbars", true);
   } else {
      alert("Need a link to test!");
   }
}

// -->
</script>
';

// if we set number of lines, make that setting here
if (!empty($li_blocklines)){
   // pad according to class chosen
   switch ($li_useclass){
      case 1:
         // smalltext class - padding 8 for IE, and 5 for all other browsers
         $li_blocklines += empty($context['browser']['is_ie']) ? 5 : 8;
         break;
      case 2:
         // normaltext class - padding 14 for IE, and 9 for all other browsers
         $li_blocklines += empty($context['browser']['is_ie']) ? 9 : 14;
         break;
      default:
         // no class - padding 10 for IE, 9 for all other browsers
         $li_blocklines += empty($context['browser']['is_ie']) ? 9 : 10;
   }
   echo   '
<div style="width: 100%; overflow: auto; height: '.$li_blocklines.'em;">
';
}

// start our form
if (empty($context['browser']['is_ie'])){
   echo "\n" . '<form name="li_form" action="' . $myself . '" method=post'.$li_classtxt.'>' . "\n";
} else {
   echo "\n" . '<form name="li_form" action="' . $myself . '" method=post>' . "\n";
}

// preset our current column to first column
$li_current_column = 1;

// start our table and first row
echo '<center><table width="90%" border="0"><tr>' . "\n";

// gets set to 1 if there is at least one radio button made next to a link
$li_link_owner = 0;

// parse our data out
while ($row = mysql_fetch_assoc($dbresult)){
   // new row?
   if ($li_current_column > $li_columns){
      // time to end current row and start new one
      $li_current_column = 1;
      echo "</tr>\n" . $li_start_row . "\n";
   }
   // each link has a column to start with
   echo ' ' . $li_start_col;
   // if they have edit or delete privileges then display checkbox
   if ($li_edit_auth || $li_del_auth){
      echo '<input type=checkbox name="li_checkbox" id="li_checkbox" value="'.$row['id'].'" /> ';
   } elseif (($user_info['username'] == $row['submitted_by']) && ($li_edit_own || $li_del_own)){
      // if they own this link in the list, and either edit own or delete own is on, display checkbox
      echo '<input type=checkbox name="li_checkbox" id="li_checkbox" value="'.$row['id'].'" /> ';
      $li_link_owner = 1;
   }
   echo '<a href="' . $row['url'] . '" title="Submitted by ' . $row['submitted_by'] . '" target=_blank>' . $row['url_name'] . '</a></td>' . "\n";
   // next column number
   $li_current_column++;
}

// end our last row and our table
echo "</tr>\n</table></center>\n";

// if there are checkboxes, add the check/uncheck all
if ($li_edit_auth || $li_del_auth || ($li_link_owner && ($li_edit_own || $li_del_own))){
   echo '<div'.$li_classtxt.'><input type=checkbox name="li_checkall" value="" onClick="invertAll(this, this.form, \'li_checkbox\');"> Check/Uncheck all</input></div>';
}

// if user is allowed to add/edit/delete links (or their own and one exists), display URL Name and URL Link edit boxes
if ($li_add_auth || $li_edit_auth || $li_del_auth || ($li_link_owner && ($li_edit_own || $li_del_own))){
   echo '<br />
      <div'.$li_classtxt.'>URL Name:</div>
      <input type=text name="li_urlname" size=' . $li_editbox_size . ' maxlength=' . $li_urlname_maxsize . '><br />
      <div'.$li_classtxt.'>URL Link:</div>
      <input type=text name="li_urllink" size=' . $li_editbox_size . '><input type=button name="li_testurl" value="Test" onClick="return urlTest()"><br /><br />
   ';
}
// if user is allowed to add links, display Add button
if ($li_add_auth){
   echo '<input type=submit name="li_add" value="Add" onClick="return addCheck()" />  ';
}
// if user is allowed to edit links (or their own and one exists), display edit link button
if ($li_edit_auth || ($li_link_owner && $li_edit_own)){
   echo '<input type=submit name="li_edit" value="Edit" onClick="return editCheck()" />  ';
}
// if user is allowed to delete links (or their own and one exists), display delete link button
if ($li_del_auth || ($li_link_owner && $li_del_own)){
   echo '<input type=submit name="li_del" value="Del" onClick="return delCheck()" />';
}

// our hidden elements
echo '<input type=hidden name="li_checklist" value="">';

// and finally, end our form
echo '</form>';

// if we added the <div> for our blocklines, close it here
if (!empty($li_blocklines))
   echo '</div>';

// free the result for good measure
mysql_free_result($dbresult);


Greets
NemWar
Title: Re: PHP-Box errors with "Link It!" Script
Post by: AngelinaBelle on May 12, 2011, 09:58:05 AM
It appears this block was meant to work with TinyPortal.
So it may take a bit of work to get it to work with SimplePortal.
Undefined variable: settings in /homepages/17/d180926987/htdocs/rockhallsailingclub/smf2test/Themes/default/languages/ManageBoards.english.php (block_edit sub template - eval?) on line 112
 This is happening because, on line 112,
Code: [Select]
$tp_prefix = $settings['tp_prefix']; -- but $settings is not defined. You should add $settings to the global statement.
HOWEVER -- since you are not running tinyportal, you are still going to have problems with $settings['tp_prefix']. That won't be defined.  It might be a good idea to use $db_prefix instead of $tp_prefix, since you don't have tinyportal installed.
 

 
Title: Re: PHP-Box errors with "Link It!" Script
Post by: NemWar on May 12, 2011, 11:56:59 AM
So, if I understand you correct, i only have to change this line?
And of course I don't use TP anymore... i switched to SP... in fact SP will be supported...  :angel:

Well, I try this now and let you know if its work...

maybe anyone also can use this little php-scipt  8)

Thanks a lot ....

NemWar

Title: Re: PHP-Box errors with "Link It!" Script
Post by: AngelinaBelle on May 12, 2011, 02:40:50 PM
I cannot guarantee, since I did not test it.
But I think that if, instead of using $tp_prefix, you simply used $db_prefix, it might just work.
Title: Re: PHP-Box errors with "Link It!" Script
Post by: NemWar on June 03, 2011, 06:36:49 AM
Hello everybody...

I find the solution by myself, so I will tell you how to get this script working...

SimplePortal has an internal PHP-Check so that not every script works...

LinkIt works (i tested it myself)

You can disable the internal PHP-Check -> here <- (http://simpleportal.net/index.php?action=docs;area=general_settings) is the Link ...

Thanks AngelinaBelle for you help ;-)

Bye
NemWar
Title: Re: PHP-Box errors with "Link It!" Script
Post by: AngelinaBelle on June 03, 2011, 01:35:15 PM
I am glad this works for you.
Yes, SimplePortal has a setting to check for bad php in the php blocks, so that a bad php block will not break your site.
Are you getting errors in your error log about settings being undefined? I think you can fix this problem.
Code: (find) [Select]
$tp_prefix = $settings['tp_prefix'];
Code: (replace) [Select]
$tp_prefix = $db_prefix;
SimplePortal 2.3.8 © 2008-2024, SimplePortal