I working on a flat-file-based project and I try to remove specific pattern from URL. The content is stored in a "content"-directory with markdown-files. I want sortable content-folder-names like:
contentfolder:
- 01-home
- 01-subpage
- 02-subpage2
- 02-page02
- 01-subpage
- 02-subpage2
etc...
At the moment, the URL would look something like this:
http://domain.com/01-home/02-subpage
This is really ugly ;)
I would prefer to get the url work as follows:
http://domain.com/home/subpage
I would prefer a solution, which works for every case of url:
http://domain.com/home/subpage/subsubpage/subsubsubpage
etc.
My script use at the moment the f3-Wildcard-Solution (GET /*). The requested URL will be replaced to get the content.
function find($path = "") {
$dirname = str_replace(globals::root(), "", globals::current());
if ($dirname == "/") {
$this->location = globals::content() . globals::home() . "/" . $path;
} else {
$this->location = globals::content() . $dirname . "/" . $path;
}
return $this;
}
Thank you guys!
Here's a solution, assuming that:
your markdown files are located in a content/ folder
every subfolder is prefixed with two digits and a hyphen (XX-something)
=>
$f3=require('lib/base.php');
$f3->set('CONTENT','content/');//location of markdown files
$f3->route('GET /*',function($f3,$params){
$path=str_replace('..','',$params[1]);//security
$dirs=glob($f3->get('CONTENT').'??-'.str_replace('/','/??-',$path));
if ($dirs) {
$dir=$dirs[0];//pick first match
echo \Markdown::instance()->convert($dir.'/default.md');
} else
$f3->error(404);
});
$f3->run();
Related
I would just like to hear if it is possible to echo something on one page, and not every other page, even though they all get content from another file.
I have 3 pages, and they all have this in code:
<?php
$path = $_SERVER['DOCUMENT_ROOT'];
$path .= "/index.php";
include_once($path);
?>
If I want "index.php" to echo something that "about.php" does not echo, can you then do it?
As the title says, I'd guess on something like this:
if filename == index.php then
echo=hello
I wouldn't make it so specific If you add more and more pages what will you do? Handle them as special cases one by one? Now you have to handle two pages, after three months they'll be 10...and so on.
It's not a perfect solution but as starting point you may use a flag. Beginning like this:
<?php
$path = $_SERVER['DOCUMENT_ROOT'];
$path .= "/index.php";
$helloWorld = TRUE;
include_once($path);
?>
And in your index.php include file:
if (helloWorld === TRUE) // or if (!!helloWorld)
echo "Hello world";
if (strpos($_SERVER['SCRIPT_NAME'],'index.php') !== false) {
echo "do stuff";
}
I'm working with an custom back-end template in Joomla and now I'm come to a point that I need to support multiple languages.
In the begin of the development I did not think about multiple languages and that gives me some trouble now. That it is best to rewrite the code and make it "smarter" I know, but at this point there is not much time to rewrite this code.
In the backend I'm working with icons that are based on the page title. For example the page Article Manager requests the icon images/icon/article-manager.png. So you see what happens if the page title is for example German and is called Inhalt.
At this moment I use this code to generate the iconpath:
#Loading Joomla Document Module
$doc = JFactory::getDocument();
#Get the page title
$title = explode("-", $doc->title);
$title = trim(end($title));
#Generate icon path
$lastTitle = explode("-", $title);
if (strpos(end($lastTitle), ':')) {
$lastTitle = explode(":", end($lastTitle));
$lastTitle = $lastTitle[0];
$iconPath = trim($lastTitle);
$iconPath = 'templates/' . $this->template . '/images/icons/' . strtolower(str_replace(" ", "-", $iconPath)) . '.png';
} else {
$iconPath = trim(end($lastTitle));
$iconPath = 'templates/' . $this->template . '/images/icons/' . strtolower(str_replace(" ", "-", $iconPath)) . '.png';
}
Now I'm thinking of searching the database for the page/componetent/modul/plugin ID but is there a faster/easier way to edit it?
Work around
As earlier stated, icons generated by a page title is indeed a bad idea. But Lodder came with a pretty nice and easy work around. So I decided to follow his idea. I added an extra variable that checks if it is a 'subpage', when yes then it extends the icon file name with this subpage.
#Loading Joomla Application Module
$app = JFactory::getApplication();
#Genrate page icon
$comp = $app->input->get('option');
$view = $app->input->get('view');
if(!empty($view)) {
$iconFileName = $comp . '-' . $view;
} else {
$iconFileName = $comp;
}
$iconPath = 'templates/' . $this->template . '/images/icons/' . $iconFileName . '.png';
Basing the icons on the Page Title is a bad idea. Purely because as you have already mentioned, your site is multi-lingual, therefore different languages will result in different results.
I would personally base the icons on the component name. For example:
$jinput = JFactory::getApplication()->input;
$view = $jinput->get('option');
The above code will output com_content and will always be the same for all languages.
You can then simply name your icon com_content.png and call the icons like so:
$iconPath = 'templates/' . $this->template . '/images/icons/' . $view . '.png';
Hope this helps
i have a large number of files with several id's in each file. For example file1.php contains a number of paragraphs, each paragraph has a unique id. (id="1",id="2",id="3" etc...) I would like the ability to create a link to a page (page A.php) and pass the location of one of these id's in the url of the link to display in a php include on page A.php The result that i'm looking for is to have the entire file (file1.php) show up inside of page A.php with the specific id that is passed in the url being highlighted. Is this possible? or do I need to use Java Script and an iframe?
Here is what I ended up with:
The url: http://mydomain/thispage.php?xul=http://mydomain.com/folder1/folder2/file.php&id=Abc150:176
The code:
Stylesheet .vrsehilite{styling}
<script type="text/javascript">var x = <?php echo json_encode($_GET["id"]); ?>;</script>
<?php
$invdmn = "<h2>Error: Invalid Domain</h2>";
$filnf = "<h2>Error: File Not Found</h2>";
$pthinv = "<h2>Error: The Path is invalid</h2>";
$idinv = "<h2>Error: The ID is invalid</h2>";
$oops = "<br/><h2>Oops! Something went wrong.<br/><br/>Please click the back button or use the menu to go to a new page.</h2>";
$testdomain = substr_compare ($_GET['xul'],"http://mydomain.com",0,20,FALSE); //make sure the domain name is correct
if ($testdomain == 0) {
$flurl = $_GET['xul'];
} else {
echo $invdmn . " " . $oops;
}
$flurl_headers = #get_headers ($flurl);
if ($flurl_headers[0] == 'HTTP/1.1 404 Not Found') {
echo $filnf . " " . $oops; //Make sure the file exist
} else {
$surl = str_replace (".com/",".com/s/",$flurl);
} //add some characters to url at point to explode
list($url1, $path) = explode ("/s/",$surl); //explode into array of 2 [0]url to domain [1] path
$testpath = substr_compare ($path,"file1/file2/",0,10,FALSE); //make sure the path is correct
if ($testpath == "0") {
$aid = preg_match ("/^[A-Z][a-z]{2}(?:[1-9][0-9]?|1[0-4][0-9]|150):(?:[1-9][0-9]?|1[0-6][0-9]|17[0-6])$/", $_GET['id']);
} else { //make sure the id is valid
echo $pthinv . " " . $oops;
}
if ($aid == 1) {
include($path);
echo "<script type='text/javascript'>";
echo "document.getElementById(x).className = 'vrsehilite';";
echo "document.getElementById(x).scrollIntoView();";
echo "window.scrollBy(0,-100);";
echo "</script>";
} else {
echo $idinv . " " . $oops;
}
?>
Never ever include arbitrary files submitted by the user. Instead, you should only include files from a pre-defined set of your choice. Perhaps something like this:
PHP
$files = array ('file1.php', 'file2.php', 'view.php', 'edit.php');
$id = (int)$_GET['id'];
if (isset ($files[$id])) {
include $files[$id];
} else {
/* Error */
}
Or you could use a regular expression to accept only certain filenames, in this case 1 or more lower case letters followed by 0 or more digits.
$m = array ();
if ( preg_match ('#^http://domain.example/folder1/folder2/([a-z]+[0-9]*\\.php)$#', $m)
&& file_exists ($m[1])) {
include $m[1];
} else {
/* Page not found */
}
You may want to check the return value of include. You may also want to move the folders into the subpattern (...) or use regular expressions for the folder names.
If all you need is to highlight a certain paragraph in a page, you should add a URL fragment that poins to the paragraph's id, and add CSS to style it. Eg:
URL
http://domain.example?id=1#p1
HTML
<p id=p1>This is the target paragraph.
CSS
p:target { /* Style the targeted <p> element */ }
I've been working on a spider algorithm and have been having some issues with the links.
example of how it works:
got content from -> example.com/bob/index.php?page=funny+faces
content is :
<html>
link 1
link 2
link 3
</html>
pass content through get links function
links function returned
[0] = ../jack/index.php
[1] = /bob_more_info
[2] = http://www.youtube.com
now I need to make these links urls by what page I got them on (example.com/bob/index.php?page=funny+faces)
so
[0] -> ../jack/index.php into example.com/jack/index.php
[1] -> /bob_more_info into example.com/bob/bob_more_info
[2] -> http://www.youtube.com
What I am asking for is a function that can do the conversion. This is mine, but it's not always working and is becoming a pain. If you could edit it or write me a function it would be much appreciated. Thanks in advance.
Here is my function currently:
//example:
//$newURL = URLfix("example.com/bob/index.php?page=funny+faces", "../jack/index.php");
function URLfix ($url, $ext)
{
if(is_valid_url($url."/"))
{
$url .= "/";
}
$ar1 = explode("/", $url);
if(count($ar1) == 1)
{
return $url."/".$ext;
}
$target = $ar1[count($ar1) - 1];
if($target == "")
{
return $url.$ext;
}
if(strpos(" ".$target, "."))
{
$cur = "";
for($i = 0; $i < count($ar1) - 1; $i ++)
{
$cur .= $ar1[$i];
$cur .= "/";
}
return $cur.$ext;
}
return $url."/".$ext;
}
use explode() to split the $url into an array delimited by /, then $bits[0] for example would contain example.com
since
example.com/jack/index.php
is equivalent to:
example.com/bob/../jack/index.php
I wouldn't worry about that part. For the url, I would remove the query string first, then pop off the last segment to get the base url:
list($url, $query_string = explode("?", $url);
$segments = explode("/", $url);
array_pop($segments);
$base_url = implode("/", $segments);
Do be sure to add some error checks.
A specification exists which explains step by step how to resolve a relative URI to it's base URI. It's RFC 3986:
What you call a "global link" is just the URI Reference.
What you call a "local link" is named Relative Reference.
Every relative reference has a base reference it refers to. The base reference is a URI reference. You can resolve a new URI reference from any base URI reference and the relative reference. This process is called Relative Resolution.
PHP code that does this, is available in the Net_URL2 PEAR Package it has an example how to use this look for ->resolve().
I am trying to scrape img src's with php, I can get the src fine, but if the src does not include the full path then I can't really reuse it. Is there a way to grab the full path of the image using php (browsers can get it if you use the right click menu).
ie. How do I get a FULL path including the domain in one of the following two examples?
src="../foo/logo.png"
src="/images/logo.png"
Thanks,
Allan
You don't need a regex... just some patience. I don't really want to write the code for you, but just check if the src starts with http://, and if not, you have like 3 different cases.
If it begins with a / then prepend http://domain.com
If it begins with .. you'll have to split the full URL and hack off pieces until the src starts with a /
Else (it begins with a letter), the take the full domain, and strip it down to the last slash then append the src URL.
Or.... be lazy and steal this script
$url = "http://www.goat.com/money/dave.html";
$rel = "../images/cheese.jpg";
$com = InternetCombineURL($url,$rel);
// Returns http://www.goat.com/images/cheese.jpg
function InternetCombineUrl($absolute, $relative) {
$p = parse_url($relative);
if($p["scheme"])return $relative;
extract(parse_url($absolute));
$path = dirname($path);
if($relative{0} == '/') {
$cparts = array_filter(explode("/", $relative));
}
else {
$aparts = array_filter(explode("/", $path));
$rparts = array_filter(explode("/", $relative));
$cparts = array_merge($aparts, $rparts);
foreach($cparts as $i => $part) {
if($part == '.') {
$cparts[$i] = null;
}
if($part == '..') {
$cparts[$i - 1] = null;
$cparts[$i] = null;
}
}
$cparts = array_filter($cparts);
}
$path = implode("/", $cparts);
$url = "";
if($scheme) {
$url = "$scheme://";
}
if($user) {
$url .= "$user";
if($pass) {
$url .= ":$pass";
}
$url .= "#";
}
if($host) {
$url .= "$host/";
}
$url .= $path;
return $url;
}
From http://www.web-max.ca/PHP/misc_24.php
Unless you have the site URL you're starting with (in which case you can prepend it to the value of the src attribute) it seems like all you're left with there is a string.
I'm assuming you don't have access to any additional information of course. If you're parsing HTML, I'd assume you must be able to access an absolute URL to at least the HTML page, but perhaps not.