I use preg_match in a function to prevent image extensions from being submitted!
Now I want to block " ~ " character also!
Can anyone tell me how I can do that?
function is_valid($url) {
$res = 1;
if (isset($url['path'])) {
if (preg_match('/\b.jpg\b/i', $url['path'])) { $res = 0; }
if (preg_match('/\b.gif\b/i', $url['path'])) { $res = 0; }
if (preg_match('/\b.png\b/i', $url['path'])) { $res = 0; }
if (preg_match('/\b.bmp\b/i', $url['path'])) { $res = 0; }
}
return $res;
}
I tried this, but it does not work:
if (strpos('~', $url['path'])) {
$res = 0;
}
First of all, you should really read something about regular expressions! If you have done that, read the manual for phps strpos.
You may try preg_match('/[^~]+\.(png|jpg|gif|bmp)/i', $url['path']) or if you want to stick to your version,
if (strpos($url['path'], '~') !== FALSE) {
$res = 0;
}
But anyway, your check will just not be very safe. Example: Someone renames a php file into png and uploads it, if you have mime_magic activated on your apache, the code will get executed. So it is much safer to check the mimetype of the file. See How do I find the mime-type of a file with php? as an example. The accepted answer there mentions a (now) deprecated function, but you can use http://de3.php.net/manual/en/function.finfo-file.php if you have PHP 5.3+
Related
I am learning to make caching and as I know
if(cache::has('cacheKey')) {
cache::get('cacheKey')
} else {
$data=cache::remember('cacheKey',time,function() {
return db::table('user')->get();});
return $data;
}
now question is when I am trying to search a specific data on key and then on result paginate that data for this I did use Cache::remember but how can make this fast when I am using all the time cache::remember function when I am trying to search in cache key, here is code
$page = $req->has('page') ? $req->query('page') : 1;
$keyword = $req->keyword;
if(cache::has('FileData')) {
if(!empty($keyword)) {
$file = cache::remember('FileData'.'search='.$keyword.'page='.$page,1440,function() use ($keyword) {
return File::with('callcase','lettercase','update_table','fileimages')
->where('id','Like','%'.$keyword.'%')
->orWhere('file_name','Like','%'.$keyword.'%')
->orWhere('recieved_by','Like','%'.$keyword.'%')
->orWhere('processed_by','Like','%'.$keyword.'%')
->orWhere('address','Like','%'.$keyword.'%')
->orWhere('contact_no','Like','%'.$keyword.'%')
->orWhere('show_status','Like','%'.$keyword.'%')
->orWhere('Reopen','Like','%'.$keyword.'%')
->orderBy('id','desc')
->paginate(25)
->setpath('');
});
$file->appends(['keyword'=>$keyword]);
if(count($file)>0) {
return view('home')->with('files',$file) ;
} else {
$error = "File does not found , Search another using keyword ";
return view('home')->with('filemsg',$error);
}
} else {
$file = cache::remember('FileData'.'page='.$page,1440,function () {
return File::with('callcase','lettercase','update_table','fileimages')
->orderBy('id','desc')
->paginate(25);});
return view('home')->with('files',$file) ;
}
} else {
$file = cache::remember('FileData',1440,function(){
return File::with('callcase','lettercase','update_table','fileimages')
->orderBy('id','desc')
->paginate(25);
});
return view('home')->with('files',$file);
}
My code is working fine and returning data as i wanted but i am confused on this when every time i am making remember data then how can i make that faster it would faster when i will retrieve data from already cache key and search in them and paginate them , How can i do this and help me if somehow i am doing wrong.
I use codeigniter 3 for my project, and I want to use redis to store session
to use it with nodejs and mysql for real-time notification system
i installed redis on my server and test it with nodejs is work good
I don't know if you are still looking for the answer(would help others too) but here's a good link (https://simplapi.wordpress.com/2012/04/13/php-and-node-js-session-share-redi/)
that explains serializing and de-serializing session data as json. I have done modification in Codeigniter's library Session_redis_driver.php to encode and decode so I can use it in Nodejs. I'd be glad if someone can validate this code and point any pitfalls or unhandled exceptions.
Modification in "read" method
public function read($session_id)
{
if (isset($this->_redis) && $this->_get_lock($session_id))
{
// Needed by write() to detect session_regenerate_id() calls
$this->_session_id = $session_id;
$session_data = json_decode($this->_redis->get($this->_key_prefix.$session_id),TRUE);
is_array($session_data)
? $this->_key_exists = TRUE
: $session_data = '';
$this->_fingerprint = md5(json_encode($session_data));
$tmp = $_SESSION;
$_SESSION = $session_data;
$new_data = session_encode();
$_SESSION = $tmp;
return $new_data;
}
return $this->_fail();
}
So far this is working without exceptions.
In write method the following changes have been done.
if (isset($this->_lock_key))
{
$this->_redis->setTimeout($this->_lock_key, 300);
$tmp = $_SESSION;
session_decode($session_data);
$new_data = $_SESSION;
$_SESSION = $tmp;
if ($this->_fingerprint !== ($fingerprint = md5(json_encode($new_data))) OR $this->_key_exists === FALSE)
{
if ($this->_redis->set($this->_key_prefix.$session_id, json_encode($new_data), $this->_config['expiration']))
{
$this->_fingerprint = $fingerprint;
$this->_key_exists = TRUE;
return $this->_success;
}
return $this->_fail();
}
return ($this->_redis->setTimeout($this->_key_prefix.$session_id, $this->_config['expiration']))
? $this->_success
: $this->_fail();
}
I am trying to combine two things for a dropdown menu in Concrete5 and just can't seem to make it work and wonder if someone can help me.
I have a this code for the menu
<?
defined('C5_EXECUTE') or die(_("Access Denied."));
$aBlocks = $controller->generateNav();
global $c;
if ($c->isEditMode()) {
echo("<div class=\"menu\" style=\"position:inherit!important;\"><ul>");
}
else {
echo("<div class=\"menu\"><ul>");
}
$nh = Loader::helper('navigation');
foreach($aBlocks as $ni) {
$_c = $ni->getCollectionObject();
if (!$_c->getCollectionAttributeValue('exclude_nav')) {
$thisLevel = $ni->getLevel();
if ($thisLevel > $lastLevel) {
echo("<!--[if IE 7]><!--></a><!--<![endif]-->\n<!--[if lte IE 6]><table><tr><td><![endif]-->\n<ul>\n");
} else if ($thisLevel < $lastLevel) {
for ($j = $thisLevel; $j < $lastLevel; $j++) {
echo("</a></li>\n</ul>\n<!--[if lte IE 6]></td></tr></table></a><![endif]--></li>\n");
}
}
if ($thisLevel == $lastLevel && $i >0) {
echo "</a></li>\n";
}
$pageLink = false;
if ($_c->getCollectionAttributeValue('replace_link_with_first_in_nav')) {
$subPage = $_c->getFirstChild();
if ($subPage instanceof Page) {
$pageLink = $nh->getLinkToCollection($subPage);
}
}
if (!$pageLink) {
$pageLink = $ni->getURL();
}
if ($_c->getCollectionAttributeValue('placeholder')) {
$pageLink="javascript:void(0)";
}
echo '<li><a href="'.$pageLink.'">' . $ni->getName();
$lastLevel = $thisLevel;
$i++;
}
}
$thisLevel = 0;
for ($i = $thisLevel; $i <= $lastLevel; $i++) {
echo("</a></li></ul>");
}
echo '</div>';
?>
but haven't had any luck styling the nav-selected so I wanted to add under the line beginning foreach:
$classes = array();
if ($ni->isCurrent) {
//class for the page currently being viewed
$classes[] = 'nav-selected';
}
if ($ni->inPath) {
//class for parent items of the page currently being viewed
$classes[] = 'nav-path-selected';
}
from another menu where the nav-selected can be styled but that doesn't make a difference. Does anyone see what I am doing wrong or could someone give me a hint how to make this work? I would really appreciate the help. Thanks!
It appears as though you've taken code from two different places and pasted them together (which rarely works). I think your first code sample is from an old template, and the second code sample is from the newer one.
Your best bet here is to start from scratch with the newer, cleaner autonav template:
https://raw.github.com/concrete5/concrete5/master/web/concrete/blocks/autonav/view.php
It shouldn't be too difficult to modify that to do what you want. If you run into trouble with that, try posting as a new question, either here or in the concrete5 forums ( http://concrete5.org/community/forums ).
I'm having trouble with my component and Joomla's SEF links. I'm trying to use JRequest::getVar to get the variables from the original URL (specified with JRoute::_)
My router.php file looks like this:
function PortfolioBuildRoute(&$query)
{
$segments = array();
if (isset($query['category'])) {
$segments[] = $query['category'];
unset($query['category']);
}
if (isset($query['subcategory'])) {
$segments[] = $query['subcategory'];
unset($query['subcategory']);
}
return $segments;
}
function PortfolioParseRoute($segments)
{
$vars = array();
$count = count($segments);
if ($count) {
$count--;
$segment = array_shift($segments);
if (is_numeric($segment)) {
$vars['subcategory'] = $segment;
} else {
$vars['category'] = $segment;
}
}
if ($count) {
$count--;
$segment = array_shift($segments) ;
if (is_numeric($segment)) {
$vars['subcategory'] = $segment;
}
}
return $vars;
}
The URL I'm encoding originally looks like:
index.php?option=com_portfolio&category=x&subcategory=y and JRoute::_ turns it into /portfolio/x/y. What I need now is some way of getting the variables x and y after the url is encoded?
----EDIT----
Ok so I figured it out - I changed the ParseRoute part of the router.php file to:
function PortfolioParseRoute($segments)
{
$vars = array();
$vars['category'] = str_replace(":", "-", $segments[0]);
$vars['subcategory'] = str_replace(":", "-", $segments[1]);
return $vars;
}
I feel I've got a slightly better understanding of the router.php file now. It turns out JRoute converts hyphens in your url to colons! Don't quite know why it picks on the poor hyphens, big JRoute bully. I could use underscores in the URL and it would function fine but hyphens are better SEO than underscores.
I used str_replace on each of the segments in ParseRoute to sort this out.
I'm not sure if this is the correct and standards way to go about this but I'm a Joomla and PHP noob so it will have to do until I'm advised otherwise.
At least it works!
:)
I was trying to make a shopping cart and got a code from web..
<?php
session_start();
require_once 'class/Item.php';
$product_id = $_REQUEST['i_id'];
$action = $_REQUEST['action'];
$item= new Item();
if($product_id && !$item->productExists($product_id)) {
die("Error. Product Doesn't Exist");
}
switch($action) {
case "add":
$_SESSION['cart'][$product_id]++;
break;
case "remove":
$_SESSION['cart'][$product_id]--;
if($_SESSION['cart'][$product_id] == 0) unset($_SESSION['cart'][$product_id]);
break;
case "empty":
unset($_SESSION['cart']);
break;
}
?>
but durring adding the first item to the cart it goes error. How can I correct it.
Error:
Notice: Undefined index: cart in C:\wamp\www\website\store_esp\addtocart.php on line 16
Notice: Undefined index: 2 in C:\wamp\www\website\store_esp\addtocart.php on line 16
Looks like you may be trying to manipulate variables that aren't setup yet. Make sure you're checking that $_SESSION['cart'][$product_id] exists before you do something on it:
if(!isset($_SESSION['cart'][$product_id]))
$_SESSION['cart'][$product_id] = 0;
switch($action) {
...
Try changing this:
$_SESSION['cart'][$product_id]++;
to this:
if (isset($_SESSION['cart'][$product_id])) {
++$_SESSION['cart'][$product_id];
} else {
$_SESSION['cart'][$product_id] = 1;
}
Without knowing the error, it's impossible to tell for sure. But using my deductive powers, I think the problem is here:
$_SESSION['cart'][$product_id]++;
It should be this:
if (isset($_SESSION['cart'][$product_id])) {
$_SESSION['cart'][$product_id]++;
} else {
$_SESSION['cart'][$product_id] = 1;
}
And you need to change this:
session_start();
// add this part
if (!isset($_SESSION['cart'])) {
$_SESSION['cart'] = array();
}
require_once 'class/Item.php';
$product_id = $_REQUEST['i_id'];
$action = $_REQUEST['action'];