edit wordpress plugin to make it front end - php

i am using a plugin for drawing on wordpress called drawblog which embed a drawing canvas at the bottom of the post page "wp-admin/post-new.php"
i am tring to find a front end solution for the drawing part to enable my users to draw without the dashboard i tried this tutorial to make a front end posting page,it worked but it only makes a form for posting and the drawing canvas does not appear.
another thing i tried was to edit the drawing plugin and make a short code to use it in a page on the website
"add_shortcode("draw-form3", "drawblog_post_form");"
and it made the canvas appear but without the ability of drawing.
following is the file of the plugin "drawblog/drawblog.php" which i tried to edit
<?php
/*
Plugin Name: DrawBlog
Plugin URI: http://drawblog.com/
Description: A WordPress plugin that allows commenters to draw a picture.
Version: 0.90
Author: Randy Tayler
Author URI: http://randytayler.com
License: GPL
*/
global $drawblog_db_version;
global $drawblog_form_complete;
global $drawblog_post_form_complete;
$drawblog_db_version = "0.63";
/* Runs when plugin is activated */
register_activation_hook(__FILE__,'drawblog_install');
// add_filter('comment_text', 'drawblog_add_image_to_comment');
// add_action('comment_form', 'drawblog_comment_form');
add_action('comment_post', 'drawblog_save_image');
add_filter('the_content', 'drawblog_add_image_to_post');
add_action('save_post', 'drawblog_save_post_image' );
add_action('edit_form_advanced', 'drawblog_post_form' );
/////tarek////
add_shortcode("draw-form3", "drawblog_post_form");
///////////
function drawblog_install() {
global $drawblog_db_version;
add_option("drawblog_db_version", $drawblog_db_version);
if (!is_dir(WP_CONTENT_DIR . '/drawblog/images')){
wp_mkdir_p(WP_CONTENT_DIR . '/drawblog/images', 755);
}
if (!get_option("drawblog_canvas_title")) add_option("drawblog_canvas_title", __('Click here to draw a picture to include in your comment.'));
if (!get_option("drawblog_hint_text1")) add_option("drawblog_hint_text1", __('Click on one of the images above to draw on it, or start from a blank canvas.'));
if (!get_option("drawblog_hint_text2")) add_option("drawblog_hint_text2", __('Include this picture with my comment.'));
if (!get_option("drawblog_warning1")) add_option("drawblog_warning1", __("This will copy over what you've already drawn. Are you sure?"));
if (!get_option("drawblog_warning2")) add_option("drawblog_warning2", __('Are you sure you want to clear your drawing?'));
if (!get_option("drawblog_canvas_width")) add_option("drawblog_canvas_width", 400);
if (!get_option("drawblog_canvas_height")) add_option("drawblog_canvas_height", 300);
if (!get_option("drawblog_post_classname")) add_option("drawblog_post_classname", drawblog_determine_classname());
if (!get_option("drawblog_show_canvas")) add_option("drawblog_show_canvas", true);
if (!get_option("drawblog_default_bg")) add_option("drawblog_default_bg", '');
}
function drawblog_add_image_to_comment($comment_text){
global $comment;
$drawblog_image = drawblog_get_image($comment->comment_ID);
if (($drawblog_image) &&
(is_file( WP_CONTENT_DIR . '/drawblog/images/' . $drawblog_image)) &&
(getimagesize( WP_CONTENT_DIR . '/drawblog/images/' . $drawblog_image) !== false)){
$comment_text = "<img src=\"". content_url(). '/drawblog/images/'.$drawblog_image."\" class=\"drawblogimage\"><br>".$comment_text;
}
return $comment_text;
}
function drawblog_add_image_to_post($post_text){
global $post;
$drawblog_image = drawblog_get_post_image($post->ID);
if (($drawblog_image) &&
(is_file( WP_CONTENT_DIR . '/drawblog/images/' . $drawblog_image)) &&
(getimagesize( WP_CONTENT_DIR . '/drawblog/images/' . $drawblog_image) !== false)){
$post_text = "<img src=\"". content_url(). '/drawblog/images/'.$drawblog_image."\" class=\"drawblogimage\"><br>".$post_text;
}
return $post_text;
}
function drawblog_get_image($comment_id){
global $wpdb;
$meta = get_comment_meta($comment_id, 'drawblog_image');
return $meta[0];
}
function drawblog_get_post_image($post_id){
global $wpdb;
$meta = get_post_meta($post_id, 'drawblog_image');
return $meta[0];
}
function drawblog_comment_form(){
global $drawblog_form_complete;
if (!$drawblog_form_complete){
echo drawblog_add_canvas();
$drawblog_form_complete = true;
}
}
function drawblog_post_form(){
global $drawblog_post_form_complete;
global $post;
global $image_exists;
$drawblog_image = drawblog_get_post_image($post->ID);
if (($drawblog_image) &&
(is_file( WP_CONTENT_DIR . '/drawblog/images/' . $drawblog_image)) &&
(getimagesize( WP_CONTENT_DIR . '/drawblog/images/' . $drawblog_image) !== false)){
$image_exists = 'true';
} else {
$image_exists = 'false';
}
if (!$drawblog_post_form_complete){
echo drawblog_add_post_canvas();
$drawblog_post_form_complete = true;
}
}
function drawblog_save_image($comment_id){
global $wpdb;
if ($_POST['drawblog_include_pic'] == true){
$data = $_POST['drawblog_picture'];
$raw_data = str_replace(' ','+',$data);
$filtered_data=substr($raw_data, strpos($raw_data, ",")+1);
$data = base64_decode($filtered_data);
$new_image = uniqid($comment_id.'_').'.png';
$fp = fopen(WP_CONTENT_DIR . '/drawblog/images/'.$new_image, 'wb' );
fwrite( $fp, $data);
fclose( $fp );
if (is_file(WP_PLUGIN_DIR . "/drawblog/icons/dbwm.png")){
$im = imagecreatefrompng(WP_CONTENT_DIR . '/drawblog/images/'.$new_image);
$src = imagecreatefrompng(WP_PLUGIN_DIR . "/drawblog/icons/dbwm.png");
list($wm_width, $wm_height) = getimagesize(WP_PLUGIN_DIR . "/drawblog/icons/dbwm.png");
imagecopy($im, $src, get_option('drawblog_canvas_width')-$wm_width, get_option('drawblog_canvas_height') - $wm_height, 0, 0, $wm_width, $wm_height);
imagepng($im, WP_CONTENT_DIR . '/drawblog/images/'.$new_image);
}
add_comment_meta($comment_id, 'drawblog_image', $new_image);
}
}
function drawblog_save_post_image($post_id){
global $wpdb;
if ($_POST['drawblog_include_pic'] == true){
$data = $_POST['drawblog_picture'];
$raw_data = str_replace(' ','+',$data);
$filtered_data=substr($raw_data, strpos($raw_data, ",")+1);
$data = base64_decode($filtered_data);
$new_image = uniqid('p'.$post_id.'_').'.png';
$fp = fopen(WP_CONTENT_DIR . '/drawblog/images/'.$new_image, 'wb' );
fwrite( $fp, $data);
fclose( $fp );
if (is_file(WP_PLUGIN_DIR . "/drawblog/icons/dbwm.png")){
$im = imagecreatefrompng(WP_CONTENT_DIR . '/drawblog/images/'.$new_image);
$src = imagecreatefrompng(WP_PLUGIN_DIR . "/drawblog/icons/dbwm.png");
list($wm_width, $wm_height) = getimagesize(WP_PLUGIN_DIR . "/drawblog/icons/dbwm.png");
imagecopy($im, $src, get_option('drawblog_canvas_width')-$wm_width, get_option('drawblog_canvas_height') - $wm_height, 0, 0, $wm_width, $wm_height);
imagepng($im, WP_CONTENT_DIR . '/drawblog/images/'.$new_image);
}
delete_post_meta($post_id, 'drawblog_image');
add_post_meta($post_id, 'drawblog_image', $new_image);
} else {
delete_post_meta($post_id, 'drawblog_image');
}
}
function drawblog_check_options(){
//new options pose a little trouble on upgrade. This'll force them to update if the installation trick didn't work
if (!get_option("drawblog_hint_text1")) add_option("drawblog_hint_text1", __('Click on one of the images above to draw on it, or start from a blank canvas.'));
if (!get_option("drawblog_hint_text2")) add_option("drawblog_hint_text2", __('Include this picture with my comment.'));
if (!get_option("drawblog_warning1")) add_option("drawblog_warning1", __("This will copy over what you've already drawn. Are you sure?"));
if (!get_option("drawblog_warning2")) add_option("drawblog_warning2", __('Are you sure you want to clear your drawing?'));
if (get_option("drawblog_hint_text1")=='') update_option("drawblog_hint_text1", __('Click on one of the images above to draw on it, or start from a blank canvas.'));
if (get_option("drawblog_hint_text2")=='') update_option("drawblog_hint_text2", __('Include this picture with my comment.'));
if (get_option("drawblog_warning1")=='') update_option("drawblog_warning1", __("This will copy over what you've already drawn. Are you sure?"));
if (get_option("drawblog_warning2")=='') update_option("drawblog_warning2", __('Are you sure you want to clear your drawing?'));
}
function drawblog_add_canvas(){
drawblog_check_options();
global $post;
if (get_option('drawblog_api_key')!=''){
$ch = curl_init('http://drawblog2.com/auth.php');
curl_setopt($ch, CURLOPT_POST, true);
$postfields = 'apikey='.get_option('drawblog_api_key').'&domain='.$_SERVER['HTTP_HOST'];
$postfields .='&theme='.get_stylesheet(); // if you're using the api, I need your theme
curl_setopt($ch, CURLOPT_POSTFIELDS, $postfields);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
echo curl_exec($ch);
curl_close($ch);
}
$width = get_option('drawblog_canvas_width');
$height = get_option('drawblog_canvas_height');
include "drawblog_canvas.php";
}
function drawblog_add_post_canvas(){
drawblog_check_options();
if (get_option('drawblog_api_key')!=''){
$ch = curl_init('http://drawblog.com/auth.php');
curl_setopt($ch, CURLOPT_POST, true);
$postfields = 'apikey='.get_option('drawblog_api_key').'&domain='.$_SERVER['HTTP_HOST'];
$postfields .='&theme='.get_stylesheet(); // if you're using the api, I need your theme
curl_setopt($ch, CURLOPT_POSTFIELDS, $postfields);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
echo curl_exec($ch);
curl_close($ch);
}
$width = get_option('drawblog_canvas_width');
$height = get_option('drawblog_canvas_height');
include "drawblog_post_canvas.php";
}
function drawblog_determine_classname(){
$themedir = get_stylesheet_directory();
$theme = get_stylesheet(); // I wish this function was called get_theme(), but whatever
switch ($theme){
case 'custom-community':
$postclass = 'post-content';
break;
case 'inferno-mf':
$postclass = 'post';
break;
case 'easel':
case 'eclipse':
case 'ifeature':
$postclass = 'entry';
break;
case 'responsive':
$postclass = "post-entry";
break;
case 'mantra':
case 'pagelines':
case 'pinboard':
case 'twentyeleven':
case 'twentyten':
case 'twentytwelve':
default:
$postclass = "entry-content";
break;
}
return $postclass;
}
function get_image_data($img){
$domain_bits = parse_url($img);
$domain = $domain_bits['host'];
$filename = $domain_bits['path'];
if ($domain == $_SERVER['SERVER_NAME']) {
$file = $img;
} else {
$file = plugins_url().'/drawblog/drawblog_safeimage.php?img='.urlencode($img);
}
$img_info = getimagesize($img);
if ($img_info !== false){
echo json_encode(array($file,$img_info[0], $img_info[1]));
} else echo '';
}
if ( is_admin() ){
add_action('admin_menu', 'drawblog_admin_menu');
function drawblog_admin_menu() {
add_options_page('DrawBlog', 'DrawBlog', 'administrator', 'drawblog', 'drawblog_settings_page');
}
function drawblog_check_api_key($apikey){
$ch = curl_init('http://drawblog.com/validate.php');
curl_setopt($ch, CURLOPT_POST, true);
$postfields = 'apikey='.$apikey.'&domain='.$_SERVER['HTTP_HOST'];
curl_setopt($ch, CURLOPT_POSTFIELDS, $postfields);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$retval = curl_exec($ch);
curl_close($ch);
return $retval;
}
function drawblog_update_options($values){
$title = $values['drawblog_canvas_title'];
$hint1 = $values['drawblog_hint_text1'];
$hint2 = $values['drawblog_hint_text2'];
$warning1 = $values['drawblog_warning1'];
$warning2 = $values['drawblog_warning2'];
$width = intval($values['drawblog_canvas_width']);
$height = intval($values['drawblog_canvas_height']);
$show_canvas = $values['drawblog_show_canvas'];
if ($values['drawblog_default_bg']) $default_bg = $values['drawblog_default_bg'];
if ($width<=0) return __('Canvas width'); //returns other than 'success', below, indicate a field with an error. Canvases cannot be negative or zero values.
if ($height<=0) return __('Canvas height');
if (($values['drawblog_api_key']!='') && (!(drawblog_check_api_key($values['drawblog_api_key']) == 1))) return __('API key is invalid or expired. Visit DrawBlog.com to purchase or renew an API key.');
update_option('drawblog_api_key', $values['drawblog_api_key']);
update_option('drawblog_canvas_width', $width);
update_option('drawblog_canvas_height', $height);
update_option('drawblog_canvas_title', $title);
update_option('drawblog_hint_text1', $hint1);
update_option('drawblog_hint_text2', $hint2);
update_option('drawblog_warning1', $warning1);
update_option('drawblog_warning2', $warning2);
update_option('drawblog_post_classname', $values['post_class_name']);
update_option('drawblog_show_canvas', $show_canvas);
update_option('drawblog_default_bg', $default_bg);
return 'success';
}
function drawblog_settings_page() {
include "drawblog_admin.php";
}
}
?>
now what is the best solution to get drawblog plugin to work on new page ?

There are some plugins allowing posting from the frontend: https://premium.wpmudev.org/blog/wordpress-post-frontend-plugins/
It might work with your drawing thing.
Hope it helps,
Ondrej

Related

Check for Valid/Active URLs before download - Laravel 7

I'm trying to check download images and store locally in my assets folder.
Before start downloading, I want to check and make sure the link is still live.
I only want to start my download if the link is live 200 Ok.
Try #1
public function handle()
{
$skills = Skill::all();
if($skills != null){
foreach($skills as $i=>$skill){
if (strpos($skill->img_path, 'http') !== false) {
if(!isset($exception)) {
//update the path in DB
$image_path = '/assets/fe/img/skill/';
$img_name = $skill->name.'.png';
$path = public_path() . $image_path . $img_name;
$uploadSuccess = file_put_contents($path, file_get_contents($skill->img_path));
// dd($uploadSuccess);
if($uploadSuccess) {
$skill->img_path = $image_path . $img_name;
}
}
}
$skill->save();
}
}
}
I seems to get so many issues
One of them is
curl: (6) Could not resolve host: thumbsplus.tutsplus.com
Another one is
file_get_contents(https://assets-cdn.github.com/images/modules/logos_page/Octocat.png): failed to open stream: HTTP request failed! HTTP/1.1 404 Not Found
What is the cleaner way ? Please suggest, I will try now.
Try #2
public function handle()
{
$skills = Skill::all();
if($skills != null){
foreach($skills as $i=>$skill){
if (strpos($skill->img_path, 'http') !== false) {
$file_headers = #get_headers($skill->img_path);
if(!$file_headers || $file_headers[0] == 'HTTP/1.1 404 Not Found') {
$exists = false;
}
else {
$exists = true;
if(!isset($exception)) {
//update the path in DB
$image_path = '/assets/fe/img/skill/';
$img_name = $skill->name.'.png';
$path = public_path() . $image_path . $img_name;
$uploadSuccess = file_put_contents($path, file_get_contents($skill->img_path));
// dd($uploadSuccess);
if($uploadSuccess) {
$skill->img_path = $image_path . $img_name;
}
}
}
}
$skill->save();
}
}
}
Try #3
public function handle()
{
$skills = Skill::all();
$failCount = 0;
$successCount = 0;
$failList = [];
if($skills != null){
foreach($skills as $i=>$skill){
if (strpos($skill->img_path, 'http') !== false) {
$file_headers = #get_headers($skill->img_path);
if(!$file_headers || strpos($file_headers[0], '404') !== false) {
$exists = false;
$failCount++;
array_push($failList,$skill->name);
// break;
}
else {
$exists = true;
$successCount++;
//DEBUG
// dd($file_headers[0]);
if(strpos($file_headers[0], '200')) {
//update the path in DB
$image_path = '/assets/fe/img/skill/';
$img_name = $skill->name.'.png';
$path = public_path() . $image_path . $img_name;
$uploadSuccess = file_put_contents($path, file_get_contents($skill->img_path));
// dd($uploadSuccess);
if($uploadSuccess) {
$skill->img_path = $image_path . $img_name;
}
}
}
}
$skill->save();
echo ".";
}
}
echo "\r\n";
$this->info('=========================');
$this->info('Success :'. $successCount);
$this->info('=========================');
$this->info('Fail :'. $failCount);
$this->info('List :'. print_r($failList));
$this->info('=========================');
}
seems to work
but it hang, sometimes more than 1 minute, at a certain dot
⚡️ php artisan skillIcons:download
..............................................................................................................................
=========================
Success :12
=========================
Fail :7
Array
(
[0] => GitHub
[1] => Geolocation API
[2] => Xcode
[3] => Protractor
[4] => Sketch
[5] => Amazon ECR
[6] => WinSCP
)
List :1
=========================
All images seems to be downloaded successfully
⚡️ ls public/assets/fe/img/skill/
AWS Console.png Digital Ocean.png Javascript.png PayPal.png Terminal.png
AWS.png Disqus.png Jest.png Photoshop.png TextMate.png
Alimofire.png Divvy.png Jira.png Pod.png TextWrangler.png
Amazon ECS.png Docker.png Kamar.png PostgreSQL.png Transmit.png
Amazon RDS.png Duet.png LESS.png PyCharm.png Twitter.png
Angular.png EC2.png Laravel Elixir.png Python.png Ubuntu.png
AngularJS.png Evernote .png Laravel.png QuickBooks.png VMWare Fusion .png
Apache.png Express.png Linode.png React Native.png VS Code.png
Atom.png Facebook.png Mac OS X.png Realm.png Vagrant.png
Bash.png Final Cut.png Markdown.png Redis.png Virtual Machine.png
BitBucket.png FusionCharts.png MobaXTerm.png RequireJS.png Virtualbox.png
Bower.png GitLab.png Mocha.png S3.png Webpack.png
CKEditor.png Go Daddy.png MySQL.png SAML 2.0.png Windows.png
CSS.png Google Chart.png NPM.png Salesforce.png Wireshark.png
Camtasia.png Google Map.png Navicat Premium.png Sass.png Word.png
Cent OS.png Google Translation.png Nginx.png Secure Shell.png Yarn.png
Chai.png Gulp.png Node.png Selenium.png iMovie.png
Chat.io.png HTML.png Noteability.png Shopify.png iOS.png
Coda.png Heroku.png OAuth 2.0.png SinonJS.png jQuery.png
CodeBox.png Illustrator.png Open Stack.png Siteground.png
Composer .png Instagram.png OpenID Connect.png Sublime Text.png
Confluence .png J Player.png PHP.png Swagger.png
3 seconds
How do I decrease the wait time to only 3 seconds ?
You have to handle error in some way.
You can try
try {
...
} catch () {
...
}
But I prefer doing things this way
public function handle()
{
Skill::get()->map(function($skill){
if(strpos($skill->img_path, 'http')) return;
$img = $this->getImageFromUrl($skill->img_path)
if(!$img == null) return;
$image_path = '/assets/fe/img/skill/';
$img_name = $skill->name.'.png';
$path = public_path() . $image_path . $img_name;
$fp = fopen($path,'x');
fwrite($fp, $img);
fclose($fp);
if($uploadSuccess) {
$skill->img_path = $image_path . $img_name;
$skill->save();
}
});
}
public function getImageFromUrl($url){
$ch = curl_init ($url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 0); // The number of seconds to wait while trying to connect. Use 0 to wait indefinitely.
curl_setopt($ch, CURLOPT_TIMEOUT, 2); // The maximum number of seconds to allow cURL functions to execute
$img = curl_exec($ch);
$err = curl_error($ch);
if($err) {
echo $err;
return null;
}
curl_close($ch);
return $img;
}
I refactored some code and if you prefer this keep it if not get only logic and do it as you prefer
Hope this helps
You may use active_url validation rule to check if the given URL is alive. According to the docs:
The field under validation must have a valid A or AAAA record according to the dns_get_record PHP function. The hostname of the provided URL is extracted using the parse_url PHP function before being passed to dns_get_record.
if (validator([$skill->img_path], ['active_url'])->fails()) {
// URL is not valid/active
}
else {
// URL is valid/active
}

Openssl_pkcs7_sign(): error opening file

it's my first time doing signing of cert using openssl. Keep hitting the above error and tried realpath() and appending file:// but still can't get openssl to sign the profile. I don't understand how this works. Any insights would be appreciated.
Edit1: I'm not sure which file is the problematic one. The error messages wasn't specific enough. Is there a way to tell?
Code and screenshots below:
function signProfile()
{
$filename = "./template.mobileconfig";
$filename = realpath($filename);
$outFilename = $filename . ".tmp";
$pkey = dirname(__FILE__) . "/PteKey.key";
$pkey = realpath($pkey);
$certFile = dirname(__FILE__) . "/CertToSign.crt";
$certFile = realpath($certFile);
// try signing the plain XML profile
if (openssl_pkcs7_sign($filename, $outFilename, 'file://'.$certFile, array('file://'.$pkey, ""), array(), 0, ""))
{
// get the data back from the filesystem
$signedString = file_get_contents($outFilename);
// trim the fat
$trimmedString = preg_replace('/(.+\n)+\n/', '', $signedString, 1);
// convert to binary (DER)
$decodedString = base64_decode($trimmedString);
// write the file back to the filesystem (using the filename originally given)
$fh = fopen($filename, 'w');
fwrite($fh, $decodedString);
fclose($fh);
// delete the temporary file
unlink($outFilename);
return TRUE;
}
else
{
return FALSE;
}
}
Remove unwanted fields if not used in
openssl_pkcs7_sign($mobileConfig, $tmpMobileConfig, $certFile, array($pkey, ""), array());
Make sure file paths are correctly supplied.
require_once('variables.php'); //stores abs path to $tmpMobileConfig/$pteKeyPath/$CertToSignPath
$prepend = "file://";
$mobileConfig = realpath("./template.mobileconfig");
$pkey = $prepend . $pteKeyPath;
$pkey = str_replace('\\', '/', $pkey);
$certFile = $prepend . $CertToSignPath;
$certFile = str_replace('\\', '/', $certFile);
$isSignedCert = openssl_pkcs7_sign($mobileConfig, $tmpMobileConfig, $certFile, array($pkey, ""), array());

Why is my variable being treated like it's null when it's being passed a value?

I've been getting this warning after defining the $image variable using the file_get_contents() function:
Warning: file_get_contents(): Filename cannot be empty
Even though I passed a value to $formname with this method call :
Image::uploadImage('postimg', "UPDATE dry_posts SET postimg = :postimg WHERE id = :postid", array(':postid' => $postid),array(':postimg' => $postimg));
$postimg is a file variable in a form. I've tried checking if the file exists, which solved the error but of course nothing was being executed. It seems to not like it whenever I use file_get_contents(), how do I turn this around?
<?php
include_once("connect.php");
class Image
{
public static function uploadImage($formname,$query,$params)
{
$formname = "";
$response = "";
$image = "";
echo 'hello';
//if(file_exists($formname))
//{
echo 'hello';
$image = base64_encode(file_get_contents($_FILES[$formname]['tmp_name']));
//}
$options = array('http'=>array(
'method'=>"POST",
'header'=>"Authorization: Bearer access code here\n".
"Content-Type: application/x-www-form-urlencoded",
'content'=>$image
));
$context = stream_context_create($options);
$imgurURL = "https://api.imgur.com/3/image";
if ($_FILES[$formname]['size'] > 10240000) {
die('Image too big, must be 10MB or less!');
}
//if(file_exists($formname))
//{
echo 'hell0';
$response = file_get_contents($imgurURL, false, $context);
//}
$response = json_decode($response);
//from
//$prearams = array($formname=>$response->data->link);
//$params = $preparams + $params;
//from changes
//$params=array(':post‌​id'=>$postid);
$params = array(':postid' => $params['postid'], ':postimg' => $params['postimg']);
connect::query($query,$params);
}
}
?>
You are unsetting "$formname" here? $formname = ""; So doesn't matter if you pass it in the method call, it will always be empty

Custom ELGG (1.8) Plugin Upload A Picture to Entity

This could be a very simple thing to do, but it is proving quite hard for me to achieve.
I am in the process of creating a custom plugin for elgg to build a library of things. I want to be able to upload an image when creating a new item.
Currently in my views/default/form/cust_plugin/save.php i have
elgg_view('input/file',array('name'=>'image','value'=>$image);
And in the actions/cust_plugin/save.php i have
$cust_plugin->image = $image;
but this doesn't work.
What am I missing, what am I doing wrong?
Thanks
To upload an image I use "file" plugin so I can manage it very simple.
In my function I call
//Load file library
elgg_load_library('elgg:file');
//Set multipart/form-data attribute on my form
$vars = array('enctype' => 'multipart/form-data');
$body_vars = file_prepare_form_vars();
//Create Array values to return
$return = array(
'title' => elgg_echo('gallery:title:add'),
'content' => elgg_view_form('elgg-gallery/uppa', $vars,$body_vars) //My form
);
My form is:
<div>
<label for="image_upload">Image upload</label>
<?php echo elgg_view('input/file', array('name' => 'img_upload')); ?>
</div>
And my action:
if (empty($_FILES['img_upload']['name']))
{
$error = elgg_echo('file:nofile');
register_error($error);
forward(REFERER);
}
//Make a file
$file = new FilePluginFile();
$file->subtype = "file";
// if no title, grab filename
if (empty($titolo))
$titolo = htmlspecialchars($_FILES['img_upload']['name'], ENT_QUOTES, 'UTF-8');
$file->title = $titolo;
$file->description = "description file";
$file->access_id = ACCESS_PUBLIC;
$file->owner_guid = elgg_get_logged_in_user_guid();
// we have a file upload, so process it
if (isset($_FILES['img_upload']['name']) && !empty($_FILES['img_upload']['name']))
{
//Generate filename
$prefix = "file/";
$filestorename = elgg_strtolower(time().$_FILES['img_upload']['name']);
$file->setFilename($prefix . $filestorename);
//Set Mimetype
$mime_type = ElggFile::detectMimeType($_FILES['img_upload']['tmp_name'], $_FILES['img_upload']['type']);
$file->setMimeType($mime_type);
//Set attributes
$file->originalfilename = $_FILES['img_upload']['name'];
$file->simpletype = file_get_simple_type($mime_type);
// Open the file to guarantee the directory exists
$file->open("write");
$file->close();
//Move file
move_uploaded_file($_FILES['img_upload']['tmp_name'], $file->getFilenameOnFilestore());
//Save file
$guid = $file->save();
//Make thumbnails
if ($guid && $file->simpletype == "image")
{
$file->icontime = time();
$thumbnail = get_resized_image_from_existing_file($file->getFilenameOnFilestore(), 60, 60, true);
if ($thumbnail)
{
$thumb = new ElggFile();
$thumb->setMimeType($_FILES['img_upload']['type']);
$thumb->setFilename($prefix."thumb".$filestorename);
$thumb->open("write");
$thumb->write($thumbnail);
$thumb->close();
$file->thumbnail = $prefix."thumb".$filestorename;
unset($thumbnail);
}
$thumbsmall = get_resized_image_from_existing_file($file->getFilenameOnFilestore(), 153, 153, true);
if ($thumbsmall)
{
$thumb->setFilename($prefix."smallthumb".$filestorename);
$thumb->open("write");
$thumb->write($thumbsmall);
$thumb->close();
$file->smallthumb = $prefix."smallthumb".$filestorename;
unset($thumbsmall);
}
$thumblarge = get_resized_image_from_existing_file($file->getFilenameOnFilestore(), 600, 600, false);
if ($thumblarge)
{
$thumb->setFilename($prefix."largethumb".$filestorename);
$thumb->open("write");
$thumb->write($thumblarge);
$thumb->close();
$file->largethumb = $prefix."largethumb".$filestorename;
unset($thumblarge);
}
}
if ($guid)
{
$message = elgg_echo("gallery:status:upsuccess");
system_message($message);
forward($guid->getURL());
}

Fatal error: Class 'google_sitemap' not found

I use google codeigniter and i want use sitemap but get following error, how can fix it?
I get this class from here: http://codeigniter.com/wiki/Google_Sitemaps
Error:
Fatal error: Class 'google_sitemap' not found in
D:\xampp\htdocs\application\controllers\sitemap_google.php on line 13
This is full code in Controller in here: D:\xampp\htdocs\application\controllers\sitemap_google.php:
<?php
class Sitemap_google extends CI_Controller
{
function My_controller()
{
parent::Controller();
$this->load->helper(array('text','url'));
$this->load->plugin('google_sitemap'); //Load Plugin
}
function index()
{
$sitemap = new google_sitemap; //This is line 13
$item = new google_sitemap_item(base_url()."MY_WEBSITE_URL",date("Y-m-d"), 'weekly', '0.8' ); //Create a new Item
$sitemap->add_item($item); //Append the item to the sitemap object
$sitemap->build("./sitemap.xml"); //Build it...
//Let's compress it to gz
$data = implode("", file("./sitemap.xml"));
$gzdata = gzencode($data, 9);
$fp = fopen("./sitemap.xml.gz", "w");
fwrite($fp, $gzdata);
fclose($fp);
//Let's Ping google
$this->_pingGoogleSitemaps(base_url()."/sitemap.xml.gz");
}
function _pingGoogleSitemaps( $url_xml )
{
$status = 0;
$google = 'www.google.com';
if( $fp=#fsockopen($google, 80) )
{
$req = 'GET /webmasters/sitemaps/ping?sitemap=' .
urlencode( $url_xml ) . " HTTP/1.1\r\n" .
"Host: $google\r\n" .
"User-Agent: Mozilla/5.0 (compatible; " .
PHP_OS . ") PHP/" . PHP_VERSION . "\r\n" .
"Connection: Close\r\n\r\n";
fwrite( $fp, $req );
while( !feof($fp) )
{
if( #preg_match('~^HTTP/\d\.\d (\d+)~i', fgets($fp, 128), $m) )
{
$status = intval( $m[1] );
break;
}
}
fclose( $fp );
}
return( $status );
}
}
$this->load->plugin('google_sitemap'); //Load Plugin
You said:
I use last version of Codeigniter.
There are no more "plugins" in Codeigniter.
It looks like you're expecting at least two classes in one file: google_sitemap_item and google_sitemap. CI's loader doesn't play well with that (it expects one class per file), so don't even bother with the CI loader, just do a straight include:
include APPPATH.'path/to/file/google_sitemap.php');
You're also using the old PHP4 constructors, which suggests you're using an older version of CI (current is 2.1.0, you can check with echo CI_VERSION;). So, this:
function My_controller()
{
parent::Controller();
$this->load->helper(array('text','url'));
$this->load->plugin('google_sitemap'); //Load Plugin
}
Should be this:
function __construct()
{
parent::__construct();
$this->load->helper(array('text','url'));
include APPPATH.'path/to/file/google_sitemap.php');
}

Categories