Image security in redactorjs editor - redactor

There is a security settings which perform a check if it actually is a picture, When a user uploads a picture.
function is_image($image_path)
{
if (!$f = fopen($image_path, 'rb'))
{
return false;
}
$data = fread($f, 8);
fclose($f);
// signature checking
$unpacked = unpack("H12", $data);
if (array_pop($unpacked) == '474946383961' || array_pop($unpacked) == '474946383761') return "gif";
$unpacked = unpack("H4", $data);
if (array_pop($unpacked) == 'ffd8') return "jpg";
$unpacked = unpack("H16", $data);
if (array_pop($unpacked) == '89504e470d0a1a0a') return "png";
return false;
}
But i can't understand how to add this code in redactorjs file to check the image. i have added this code on image-processor.php but no result.I have added this code:
if (is_image($_FILES['file']['path']) == false){
die("Not an image at all");
};
Not sure is it working cause, if i add a movie instead of an image then the file get upload first and then happens nothing! i think it is a problem for server, what i want is to do something without being upload the file?

Related

Drupal 8 | Wrong alias used

Bonjour,
I have a problem on Drupal 8 that I can't solve, that's why I'm calling on you.
I have 2 aliases for the same node :
/public/event/10
/pro/event/10
I have a block_1 that only appears on the " /public/* " pages and a block_2 on the " /pro/* " pages.
When I access to the url "/pro/event/10", block_1 is displayed and not block_2.
I conclude that Drupal selects the alias "/public/event/10" (probably the first one he finds) while I'm on the page "/pro/event/10".
How can I programmatically tell Drupal the right alias to use?
Thank you in advance for your help.
Here is the code if it can help someone
class OOutboundPathProcessor implements OutboundPathProcessorInterface
{
function processOutbound($path, &$options = [], Request $request = NULL, BubbleableMetadata $bubbleable_metadata = NULL)
{
// Only for nodes
if (!isset($options['entity_type']) OR $options['entity_type'] !== 'node')
{
return $path;
}
// Get current 'space'
$espace = \Drupal::service('session')->get('espace');
// Get the node to process
$node = $options['entity'];
// New path
$str_path = "/%s/%s/%s";
$new_path = sprintf($str_path, $espace, $node->bundle(), $node->id());
// Check new path
$isValid = \Drupal::service('path.validator')->isValid($new_path);
if ($isValid === true) return $new_path;
return $path;
}
}
You might want to create your own path_processor_outbound service by implementing OutboundPathProcessorInterface.
This implementation may work on /node/{id} paths if the current requests path matches /public/event/** or /pro/event/**.
Analyzing the node entity for it's type (bundle): If it is event generate and return your desired path; if it is not event don't manipulate the path and return the original.
Writing the actual implementation in PHP code may be your own pleasure ;-)

Drupal 7, save new node while editing old

My need is to change behavior of edit form, for several content types.
The objective is:
-After update button has been pressed, don't update the node but create a new one with values from old node. I could do that by passing old node's fields values to "/node/add/my_content" form but this require a lot of work (the forms are quite complicated) and on edit page i have already all values ready in my fields.
So i already tried hook_form_alter
function mymodule_form_alter (&$form, &$form_state, $form_id) {
switch ($form_id) {
case 'my_not_update_form':
$node = $form_state['node'];
if (!isset($node->nid) || isset($node->is_new)) {
// This is a new node.
}
else {
$new_node = new StdClass();
$new_node->type = 'my_not_update_form';
node_object_prepare($new_node);
$new_node->uid = $user->uid;
$new_node->language = 'und';
$new_node->title = NULL;
$form['vid']['#value'] = NULL;
$form['nid']['#value'] = NULL;
$form['created']['#value'] = $new_node->created;
$form['changed']['#default_value'] = NULL;
$form['#node'] = $new_node;
$form['#entity'] = $new_node;
$form_state['node'] = $new_node;
$form_state['build_info']['args'][0] = $new_node;
}
break;
}
}
So with the above code i'm able to create a new node but the "create date" parameter always stay the same as create date parameter of an old node and none of the above line can solve that problem.
If you want to create an entirely new node when you submit edits to an existing node, then you want to use hook_node_presave(), which allows you to set any property of the node object before it's saved to the database.
In this example unsetting the nid and vid, and explicitly setting the is_new property will achieve this:
function my_module_node_presave($node) {
unset($node->nid);
unset($node->vid);
$node->is_new = TRUE;
}
This will leave the existing node untouched and unedited, and will instead create an entirely new node.
So to fully change the behavior of form update i give up on hook_form_alter() and instead i used hook_node_presave
function mymodule_node_presave($node) {
if($node->is_new == FALSE || isset($node->nid)) {
unset($node->nid);
unset($node->vid);
unset($node->vuuid);
$node -> created = time();
$node -> timestamp = time();
$node-> is_new = TRUE;
$node -> changed = time();
unset($node->revision_timestamp);
unset($node->num_revisions);
unset($node->current_revision_id);
unset($node->is_current);
unset($node->is_pending);
unset($node->revision_moderation);
unset($node->date);
unset($node->vuuid);
unset($node->data);
}
}

registry key exists disable button

no matter what i do if the key is there the button dont disable and i have no idea why this is the code i have for it currently
//2012
using (RegistryKey Key = Registry.LocalMachine.OpenSubKey(#"HKEY_LOCAL_MACHINE\SOFTWARE\Autodesk\Maya\2012"))
{
if (Key != null)
{
maya8.Text = "Maya 2012 Installed";
maya8.Enabled = false;
}
else
{
maya8.Text = "Install maya 2012";
maya8.Enabled = true;
}
}
using (RegistryKey Key = Registry.LocalMachine.OpenSubKey(#"HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Autodesk\Maya\8.5"))
{
if (Key != null)
{
maya8.Text = "Maya 8.5 Installed";
maya8.Enabled = false;
}
else
{
maya8.Text = "Install maya 8.5";
maya8.Enabled = true;
}
}
}
and no matter that i do i get this issue and here is a screen of registry
image of program when launched
thank you in advance elfenliedtopfan5
See that you're referencing the local machine key, and then opening a sub key of that local machine key. The path to the sub key should be relative to the local machine key. That is, instead of the following:
Registry.LocalMachine.OpenSubKey(#"HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Autodesk\Maya\8.5"))
Try this:
Registry.LocalMachine.OpenSubKey(#"SOFTWARE\Wow6432Node\Autodesk\Maya\8.5"))
The answer was discovered here by simply Googling for examples of the usage of this function!
Also note that your links aren't quite correct in your post. Check your markdown; note that you get a preview before posting!

PhpExcel - How to display or export a chart already in excel

I would like to use PHPExcel to save some charts that already exist in an excel file as image files (Save as Image).
Is this possible and can anyone point me to some sample code as Google results only bring back code to generate new charts from numeric data.
You should look at the example 35chartrender.php in the /Tests directory, it does exactly what you want. Basically it all comes down to $chart->render($jpegFileName);. I don't think there is any way to render a chart as anything else than a jpeg, though you could always ask the expert at http://phpexcel.codeplex.com/.
If these are actual images rather than MS Excel charts, then take a look at section 4.6.37 of the developer documentation, entitled "Reading Images from a worksheet":
$i = 0;
foreach ($objPHPExcel->getActiveSheet()->getDrawingCollection() as $drawing) {
if ($drawing instanceof PHPExcel_Worksheet_MemoryDrawing) {
ob_start();
call_user_func(
$drawing->getRenderingFunction(),
$drawing->getImageResource()
);
$imageContents = ob_get_contents();
ob_end_clean();
switch ($drawing->getMimeType()) {
case PHPExcel_Worksheet_MemoryDrawing::MIMETYPE_PNG :
$extension = 'png'; break;
case PHPExcel_Worksheet_MemoryDrawing::MIMETYPE_GIF:
$extension = 'gif'; break;
case PHPExcel_Worksheet_MemoryDrawing::MIMETYPE_JPEG :
$extension = 'jpg'; break;
}
} else {
$zipReader = fopen($drawing->getPath(),'r');
$imageContents = '';
while (!feof($zipReader)) {
$imageContents .= fread($zipReader,1024);
}
fclose($zipReader);
$extension = $drawing->getExtension();
}
$myFileName = '00_Image_'.++$i.'.'.$extension;
file_put_contents($myFileName,$imageContents);
}
If they are actual MS Excel charts, then (as Technoh has suggested) the chart render() method as demonstrated in example 35chartrender.php will use jpgraph to create an image file from that chart definition.

WMF image data validation?

There is an image capturing device which gives its output in wmf. This output is stored in the database directly. We have cases where at times some of these images do not appear on a web page in IE. But if we right click on the page we are able to save the image on to the hard disk; meaning the image does exist on the page, but does not appear visible. I think this is because of some file corruption issue, but I don't know how to resolve it. We are however able to view such files using MS Picture Viewer (desktop app). Is there anyway we can detect such problematic files?
I hope I am not being over simplistic over this but the following function works for me:
public bool IsValidMetaFile(string filePath)
{
try
{
var metaFile = new Metafile(filePath);
var metaFileHeader = metaFile.GetMetafileHeader();
return metaFileHeader.IsWmf() ||
metaFileHeader.IsWmfPlaceable() ||
metaFileHeader.IsEmf() ||
metaFileHeader.IsEmfPlusDual() ||
metaFileHeader.IsEmfPlusOnly() ||
metaFileHeader.IsEmfOrEmfPlus();
}
catch (Exception mesg)
{
return false;
}
}

Resources