We help IT Professionals succeed at work.
Get Started

How do I insert this php code into my php file?

badwolfff
badwolfff asked
on
425 Views
Last Modified: 2012-05-11
I have a working version (modified) of the Sige plugin for Joomla 1.6
which can be downloaded from:
http://extensions.joomla.org/extensions/photos-a-images/photo-gallery/13762

Within this I have custom added a script which shortens long URLs manually.
The file sige.php is attached here:  sige.php
I shorten the long URLs that this plugin generates (for the facebook/twitter buttons) with the following code at the top of this file (lines 70-99):
function make_bitly_url($url,$login,$appkey,$format = 'xml',$version = '2.0.1') {
	$sql = "SELECT * FROM `url_cache` WHERE `before`='".mysql_real_escape_string($url)."' LIMIT 1";
	$check = mysql_query($sql) or die(mysql_error());
	if(mysql_num_rows($check)==1) {//we found it in cache
		$url = mysql_fetch_assoc($check);
		return $url['after'];
	}

	//create the URL
	$bitly = 'http://api.bit.ly/shorten?version='.$version.'&longUrl='.urlencode($url).'&login='.$login.'&apiKey='.$appkey.'&format='.$format;

	//get the url
	//could also use cURL here
	$response = file_get_contents($bitly);

	//parse depending on desired format
	if(strtolower($format) == 'json') {
		$json = @json_decode($response,true);
		$sql = "INSERT INTO `url_cache` SET `before`='".mysql_real_escape_string($url)."', `after`='".mysql_real_escape_string($json['results'][$url]['shortUrl'])."'";
		mysql_query($sql) or die(mysql_error());
		return $json['results'][$url]['shortUrl'];
	}
	else //xml
	{
		$xml = simplexml_load_string($response);
		$sql = "INSERT INTO `url_cache` SET `before`='".mysql_real_escape_string($url)."', `after`='".mysql_real_escape_string('http://bit.ly/'.$xml->results->nodeKeyVal->hash)."'";
		mysql_query($sql) or die(mysql_error());
		return 'http://bit.ly/'.$xml->results->nodeKeyVal->hash;
	}
}

Open in new window


Later I call this code to shorten my URLs in real time with this code (lines 804-810):
$url = $mosConfig_live_site."/plugins/content/sige/plugin_sige/socialshare.php?img=".$mosConfig_live_site.$rootfolder.$_images_dir_."/Midsize/".$images[$a]['filename']."&name=".$imagename;

$u = make_bitly_url($url,'badwolfff','R_f650e553b78bb0569fc67dbc16a5de7c','json');

$html .= '<a href="http://www.facebook.com/sharer.php?u='.$u.'&t=Zuenelli - '.$imagename.'"  title="aggiungi a facebook" target="_blank"><img src="'.$mosConfig_live_site.'/plugins/content/sige/plugin_sige/facebook.png" /></a><a href="http://twitter.com/?status=Zuenelli propone - '.$imagename.' (cliccare sul seguente link per scoprire): '.$u.'"  title="pubblica su twitter" target="_blank"><img src="'.mosConfig_live_site.'/plugins/content/sige/plugin_sige/twitter.png" /></a>

Open in new window


The working versions can be tested on the following link (try clicking on the facebook/twitter links):
My test website

The problem is this:
at present I use the bit.ly service with my api login/key but I would like to use a custom url shortening service which runs from my own server.

I succeeded in doing this by following another web tutorial.
The code is as follows:
- short.php
<?php
$baseurl = 'http://localhost/'; //address to use as the first part of the short URLs (note trailing slash)
$sql_host = 'localhost'; //host of your mysql database (usually localhost)
$sql_user = 'root'; //mysql user with access to your database
$sql_pass = 'root'; //password for this user
$sql_db = 'turl'; //the name of your database
 
$db = mysql_connect($sql_host, $sql_user, $sql_pass);
mysql_select_db($sql_db, $db);
 
if(isset($_GET['k'])) {
   $k = mysql_real_escape_string($_GET['k'], $db);
   if($result = mysql_query("SELECT `url` FROM `turl` WHERE `key` = '" . $k . "'", $db)) {
      if(mysql_num_rows($result) > 0) {
         $row = mysql_fetch_row($result);
         header('HTTP/1.1 301 Moved Permanently');
         header('Location: ' . $row[0]);
         exit;
      }
   }
}
 
if(isset($_POST['submit'], $_POST['url'])) {
   if(filter_var($_POST['url'], FILTER_VALIDATE_URL, FILTER_FLAG_HOST_REQUIRED) !== false) {
      $url = mysql_real_escape_string($_POST['url'], $db);
      if($result = mysql_query("SELECT `key` FROM `turl` WHERE `url` = '" . $url . "'", $db)) {
         if(mysql_num_rows($result) > 0) {
            $row = mysql_fetch_row($result);
            $key = $row[0];
         }
      }
      if(!isset($key) && mysql_query("INSERT INTO `turl` (`url`) VALUES ('" . $url . "')", $db)) {
         $id = mysql_insert_id($db);
         $key = base_convert($id, 10, 36);
         mysql_query("UPDATE `turl` SET `key` = '" . $key . "' WHERE `id` = '" . $id . "'");
      }
      echo '<a href="' . $baseurl . $key . '" target="_blank">' . $baseurl . $key . '</a>';
   }
}
else {
?>
<form method="post" action="short.php">
<label>URL: <input type="text" name="url" id="url" /></label>
<br />
<input type="submit" name="submit" id="submit" value="Shorten" />
</form>
<?php
}
?>

Open in new window


.htaccess
RewriteEngine On
RewriteCond %{REQUEST_URI} \/([0-9a-z]+)$ [NC]
RewriteRule ^(.*) short.php?k=%1 [L]

Open in new window


sql code
CREATE TABLE `turl` (
  `id` INT(11) NOT NULL AUTO_INCREMENT,
  `key` VARCHAR(32) DEFAULT NULL,
  `url` text NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `key` (`key`)
);

Open in new window


A working example can be seen here:Working URL shortening example

**************************************************************************************************************

What I would like to do is to substitute the bit.ly short URL generation code in my sige.php with the working code from short.php. Sige.php generates URLs on the fly and I need my new code with the short.php script to generate all shortened URLs on the fly as my icons get created from my images folder and loaded on my page (same as how it works now with the bit.ly code).

How could I manage that? I am not a php expert so I would really appreciate the exact code that I need to modify/substitute in my sige.php document.

thanks in advance

p.s. I also assume that after I manage to do that I will have to leave the .htaccess file inside the
JoomlaSite/plugins/content/sige
folder - right?
Comment
Watch Question
Most Valuable Expert 2011
Author of the Year 2014
Commented:
This problem has been solved!
Unlock 1 Answer and 8 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE