Gravatar Image Still Not Loading Open Cart Admin - php

Hi I am trying to get my gravatar api working with my open cart admin/controller/common/header.php and my admin/view/template/common/header.tpl
Still not working gave it ago before that some one gave me advice on but now not working? So thought give it ago another way but nothing.
admin / controller/ header. php
This is just trimmed down version
<?php
class ControllerCommonHeader extends Controller {
protected function index($get_gravatar) {
}
function get_gravatar( $email, $s = 80, $d = 'mm', $r = 'g', $img = false, $atts = array() ) {
$url = 'http://www.gravatar.com/avatar/';
$url .= md5( strtolower( trim( $email ) ) );
$url .= "?s=$s&d=$d&r=$r";
if ( $img ) {
$url = '<img src="' . $url . '"';
foreach ( $atts as $key => $val )
$url .= ' ' . $key . '="' . $val . '"';
$url .= ' />';
}
return $url;
}
admin / view / template / common / header.tpl
<?php
$email = $user_info['email']; // Not Working "Need it to pick up who ever logins"
$email = "your#rmail.com"; // Works
$default = "http://www.somewhere.com/homestar.jpg";
$size = 150;
?>
<li>
<a href="" class="text-center">
<img src="<?php echo $grav_url = "http://www.gravatar.com/avatar/" . md5( strtolower( trim( $email ) ) ) . "?d=" . urlencode( $default ) . "&s=" . $size;; ?>" alt="" />
</a>
</li>

Changes for getting gravatar image in header.tpl
Update system/library/user.php as below:
After: $this->username = $user_query->row['username'];
Add: $this->email = $user_query->row['email'];
Before: public function getUserName() {
Add:
public function getUserEmail() {
return $this->email;
}
Update admin/controller/common/header.php as below:
After: $this->data['logged'] = sprintf($this->language->get('text_logged'), $this->user->getUserName());
Add: $this->data['email'] = $this->user->getUserEmail();
Update admin/view/template/common/header.tpl as below:
<div class="img-circle">
<img src="http://www.gravatar.com/avatar/<?php echo md5(strtolower(trim($email))); ?>">
</div>
Please let me know the result of these changes.
Note: In opencart, you need to assign values to variables like: $this->data['variable_name'] in controller files and use them template files like: $variable_name.

Have you tried adding an extension to your url => www.gravatar.com/avatar/far512q3tgfqwe*.jpg* for example, a quick google search and i came up with this url, check it for further info:
http://en.gravatar.com/site/implement/images/
Try this piece of code in your header.php to get the email of the current logged in user:
$this->load->model('user/user');
$email_data = $this->model_user_user->getUser($this->user->getId());
$email = $email_data['email'];
if you want to get emails for all users it need to be handled differently.

Related

why get 404 error when the PHP echo position is changed

<?php
define('PATH', dirname(dirname(__FILE__)).'/');
require_once (PATH.'./wp-blog-header.php');
global $wpdb;
// if(!isset($_POST['username'])){
// //echo $_POST['submit'];
// exit('非法访问!');
// }
$username = $_POST["username"];
$password = $_POST["password"];
$email = $_POST["email"];
$checkcode = $_POST["checkcode"];
$query_pwd_str = "SELECT password FROM yjhy_users_custom WHERE username=%s";
if($wpdb == null)
echo "wpdb is null";
$pwd_query_result = $wpdb->get_results($wpdb->prepare($query_pwd_str, $username));
$pwd_query_result_count = count($pwd_query_result);
if($pwd_query_result_count == 1){
//已经存在该用户名,返回数据
// error here
header('Content-type:text/json');
$json_ouput = '{"status":"success","errormsg":"'.$username.'"}';
echo $json_ouput;
}
?>
With the code above, I use method post to refer to the php file, and it ALWAYS responsed with 404 Error.
But, when I modify PHP code as BELOW:
<?php
//****************** move the function header() here
header('Content-type:text/json');
//****************** echo "{" here
echo "{";
?>
<?php
define('PATH', dirname(dirname(__FILE__)).'/');
require_once (PATH.'./wp-blog-header.php');
global $wpdb;
// if(!isset($_POST['username'])){
// //echo $_POST['submit'];
// exit('非法访问!');
// }
$username = $_POST["username"];
$password = $_POST["password"];
$email = $_POST["email"];
$checkcode = $_POST["checkcode"];
$query_pwd_str = "SELECT password FROM yjhy_users_custom WHERE username=%s";
if($wpdb == null)
echo "wpdb is null";
$pwd_query_result = $wpdb->get_results($wpdb->prepare($query_pwd_str, $username));
$pwd_query_result_count = count($pwd_query_result);
if($pwd_query_result_count == 1){
//已经存在该用户名,返回数据
// error here
$json_ouput = '"status":"success","errormsg":"'.$username.'"';
echo $json_ouput;
}
?>
<?php
//****************** echo "}" here
echo "}";
?>
AND THEN, it works, the server response status is 200!
I'm puzzled about this problem, and have searched all day and can't find the answer!
Why does the code work like this?
With 1 days' time, I find it out!
I request the link with the file path http://localhost:7770/api/register.php directly rather than request link by creating a page in the Dashboard, so the link is not in the WP's database.
And WP framework need to initialize itself when execute wp(); in the require_once (PATH.'./wp-blog-header.php');, but when the initial action execute in the file /wp-includes/class-wp.php with code below:
public function main($query_args = '') {
$this->init();
**$this->parse_request($query_args);**
$this->send_headers();
$this->query_posts();
$this->handle_404();
$this->register_globals();
do_action_ref_array( 'wp', array( &$this ) );
}
}
the statement $this->query_posts(); need to check the link $this->parse_request();. the func parse_request():
public function parse_request($extra_query_vars = '') {
global $wp_rewrite;
/**
* Filter whether to parse the request.
*
* #since 3.5.0
*
* #param bool $bool Whether or not to parse the request. Default true.
* #param WP $this Current WordPress environment instance.
* #param array|string $extra_query_vars Extra passed query variables.
*/
if ( ! apply_filters( 'do_parse_request', true, $this, $extra_query_vars ) )
return;
$this->query_vars = array();
$post_type_query_vars = array();
if ( is_array( $extra_query_vars ) ) {
$this->extra_query_vars = & $extra_query_vars;
} elseif ( ! empty( $extra_query_vars ) ) {
parse_str( $extra_query_vars, $this->extra_query_vars );
}
// Process PATH_INFO, REQUEST_URI, and 404 for permalinks.
// Fetch the rewrite rules.
$rewrite = $wp_rewrite->wp_rewrite_rules();
if ( ! empty($rewrite) ) {
// If we match a rewrite rule, this will be cleared.
***$error = '404';***
$this->did_permalink = true;
$pathinfo = isset( $_SERVER['PATH_INFO'] ) ? $_SERVER['PATH_INFO'] : '';
list( $pathinfo ) = explode( '?', $pathinfo );
$pathinfo = str_replace( "%", "%25", $pathinfo );
list( $req_uri ) = explode( '?', $_SERVER['REQUEST_URI'] );
$self = $_SERVER['PHP_SELF'];
$home_path = trim( parse_url( home_url(), PHP_URL_PATH ), '/' );
And the link is not in the database, so WP set the error 404 here below:
And then I get the error 404in ***$error = '404';***!
resolve solution:
create a page in the Dashboard.
modify the page url you want.
put your code in the page template.
use this template in the page.
and Ok

How to check that value exist in YII Session Variable

I am using yii and creating a cart, by using id of product i need to check that id already exists or not , but i use in_array and array_key_exists but unable to solve it Here is my code of controller
public function actionCartupdateajax() {
//start yii session
$session = Yii::app()->session;
// get posted values
$id = isset($_POST['id']) ? $_POST['id'] : "";
$name = isset($_POST['name']) ? $_POST['name'] : "";
$price = isset($_POST['price']) ? $_POST['price'] : "";
$imgSrc = Yii::app()->request->baseUrl . '/images/icondeletecart.png';
/*
* check if the 'cart' session array was created
* if it is NOT, create the 'cart' session array
*/
if (!isset($session['cart_items']) || count($session['cart_items']) == 0) {
Yii::app()->session['cart_items'] = array();
}
/*
* Here is the proble
* check if the item is in the array, if it is, do not add
*/
if (in_array($id, Yii::app()->session['cart_items'])) {
echo 'alreadyadded';
} else {
Yii::app()->session['cart_items'] = $id;
echo '<li><strong>' . $name . '</strong><span>' . $price . '</span>'
. '<img src=' . $imgSrc . ' alt="No Image" class="imagedeletecart" id=' . $id . '></li>';
}
}
and the error in console is
in_array() expects parameter 2 to be array, string given
I think problem in next row:
Yii::app()->session['cart_items'] = $id;
After this code cart_items will be NOT array, but integer or string.
Clear session and try to change:
Yii::app()->session['cart_items'][] = $id;
And better use CHtml for generation html. It is cleaner. Like this:
echo CHtml::tag('li', array(/*attrs*/), 'content_here');
//your code
echo '<li><strong>' . $name . '</strong><span>' . $price . '</span>'
. '<img src=' . $imgSrc . ' alt="No Image" class="imagedeletecart" id=' . $id . '></li>';
//I propose this way(but you can use your version):
echo CHtml::tag(
'li',
array(),
CHtml::tag(
'strong',
array(),
'name'
) . CHtml::tag(
'span',
array(),
'price'
) . CHtml::image(
'src',
'alt',
array(
'class' => 'imagedeletecart',
'id' => 'id'
)
)
);

How to embed flickr in silverstripe by shortcodes

Hello,
I'm implementing some "shortcodes" in my silverstripe website. For example, i already created some for Youtube, Vimeo and Soundcloud but i can't find a way to add the Flickr one.
Here is a sample code for vimeo :
public static function Vimeo($args, $caption = null, $parser = null) {
if (empty($args['id']))
return;
$data = array();
$data['VimeoID'] = $args['id'];
$data['autoplay'] = false;
$data['caption'] = $caption ? Convert::raw2xml($caption) : false;
$data['width'] = 640;
$data['height'] = 385;
$data = array_merge($data, $args);
$template = new SSViewer('shortcode/Vimeo');
return $template->process(new ArrayData($data));
And this is what i found for flickr :
$query = "http://api.flickr.com/services/rest/?method=flickr.photos.getInfo&api_key=" . API_KEY . "&photo_id=" . $photoid . "&format=json&nojsoncallback=1";
data = json_decode(file_get_contents($query));
echo "created by: " . data->photo->owner->username;
echo "link to photopage: " . "http://www.flickr.com/photos/" . data->photo->owner->nsid
But nothing for the .ss file
Does anybody know how to do it or already did it?
Thanks for help!
Thomas.
Hi
I finally found a solution for using the flickr shortcode ->
public static function Flickr($args, $caption = null, $parser = null) {
if (empty($args['set_id']) && empty($args['user_id']))
return;
$data = array();
$data['SET'] = $args['set_id'];
$data['USER'] = $args['user_id'];
if (!$scid = SiteConfig::current_site_config()->FlickrClientID) {
$data['KEY']= $scid = SiteConfig::current_site_config()->FlickrClientID = "id";
}
ini_set("flickr", "FLICKR");
$pics = json_decode(file_get_contents(
"http://www.flickr.com/services/rest/?method=flickr.photos.getAllContexts&api_key=".$scid."&format=json&set_id=".$args['set_id']), true);
$data['ID'] = $pics['id'];
$data = array_merge($data, $args);
$template = new SSViewer('shortcode/Flickr');
return $template->process(new ArrayData($data));
}
Using this iframe ->
<div class='Flickr clearfix'>
<iframe align="center" src="http://www.flickr.com/slideShow/index.gne?user_id=$USER&set_id=$SET" frameBorder="0" width="500" height="500" scrolling="no"><br /></iframe>
</div>
This solution works fine if you want to display some flickr set pictures.
Hope it'll help someone else.
See you

How to display the Facebook profile photo in my php project

In my project I am signing in with a Facebook account, but the profile page displays the image from gravatar.com. I want to display the profile image from www.facebook.com of registered users.
In my local system I have the following code to display profile photo from www.gravatar.com
public function gravatar($email, $s = 80, $d = 'mm', $r = 'g', $img = FALSE, $atts = array())
{
$url = 'https://secure.gravatar.com/avatar/'
. md5(strtolower(trim( $email)))
. "?s=$s&d=$d&r=$r";
if ($img)
{
$url = '<img src="' . $url . '"';
foreach ($atts as $key => $val)
{
$url .= ' ' . $key . '="' . $val . '"';
}
$url .= ' />';
}
return $url;
}
How can I display the Facebook profile photo instead of gravatar.com profile photo?
You can link to https://graph.facebook.com/usernameOrId/picture.
Something like this:
function facebookAvatar($uid, $img = FALSE, $atts = array())
{
$url = 'https://graph.facebook.com/'.trim($uid).'/picture';
if ($img)
{
$url = '<img src="' . $url . '"';
foreach ($atts as $key => $val)
{
$url .= ' ' . $key . '="' . $val . '"';
}
$url .= ' />';
}
return $url;
}
and to get the Image use:
<?php echo facebookAvatar('UsernameOrUserID', TRUE, array('width'=>'80px', 'height'=>'80px')); ?>
UsernameOrUserID you've to change to the UserID:
1234321
if the url is:
http://facebook.com/profile.php?id=1234321
or to the username:
my.name
if the facebook url is http://facebook.com/my.name
If you are using PHP SDK you can get the userId of the current logged in USer and display the user Image.
You can do like this:
require_once 'path/to/facebookSDK.php';
$facebook = new Facebook(array(
'appId' => 'YOUR_APP_ID',
'secret' => 'YOUR_APP_SECRET',
));
// Get User ID
$user = $facebook->getUser();
//this will ouptput your current logged in user facebook image
echo getFacebookPhotoAvatar ($user);
function getFacebookPhotoAvatar ($user) {
if($user) {
$photoUrl = "https://graph.facebook.com/".$user."/picture";
$facebookPhoto = '<img src="'.$photoUrl.'" alt="FacebookUserImage"/>';
} else {
$facebookPhoto = '';
}
return $facebookPhoto ;
}
Hope this helps :)

Zend link helper exists?

Some other frameworks have a link helper like output_link('anchor', 'destination'); to replace the need to type into the template. Does Zend have something similar? and do I have to declare the link in the action first before I can use it in the viewer?
Zend_View_Helper_Url can generate URL in view, take a look on its API doc
http://framework.zend.com/apidoc/core/Zend_View/Helper/Zend_View_Helper_Url.html
I am not sure if Zend has this, but all you would need to do is create your own outputLink in the View Helper (applications/views/helpers/) and set it up how you want to, should be pretty trivial.
class Zend_View_Helper_OutputLink extends Zend_View_Helper_Abstract
{
public function outputLink($anchor, $description)
{
return '' . $description . '';
}
}
Just modify it how you want to. And you would call it in your view like below:
<span><?php $this->outputLink('test.html', 'Test Me!'); ?> </span>
Here's my anchor element view helper for zend. You need to use my image element view helper or remove the part of the code that uses it in case you don't like it. Of course, you're free to modify name and whatever else you wish.
require_once 'Zend/View/Helper/HtmlElement.php';
class Ecoweb_View_Helper_AnchorElement extends Zend_View_Helper_HtmlElement {
/**
*
* #param string $url
* #param string $content
* #param array|string $attribs
* #return string
*/
public function anchorElement($url, $content = '', $attribs = null)
{
if (is_array($url)) {
$reset = isset($url[2]) ? $url[2] : false;
$encode = isset($url[3]) ? $url[3] : false;
$url = $this->view->url($url[0], $url[1], $reset, $encode);
} else {
$url = $this->view->baseUrl($url);
}
if (is_array($attribs)) {
$attribs = $this->_htmlAttribs($attribs);
} else {
$attribs = empty($attribs) ? '' : ' '.$attribs;
}
if (is_array($content) && isset($content['src'])) {
$src = $content['src'];
$alt = isset($content['alt']) ? $content['alt'] : null;
$imgAttribs = isset($content['attribs']) ? $content['attribs'] : array();
$content = $this->view->imgElement($src, $alt, $imgAttribs);
}
$content = empty($content) ? $url : $this->view->escape($content);
$xhtml = '<a '
. 'href="'.$url.'"'
. $attribs
. '>'
. $content
. '</a>';
return $xhtml;
}
}
Here's the image element view helper:
<?php
require_once 'Zend/View/Helper/HtmlElement.php';
class Ecoweb_View_Helper_ImgElement extends Zend_View_Helper_HtmlElement {
/**
*
* #param string $src
* #param string $alt
* #param array|string $attribs
* #return string
*/
public function imgElement($src, $alt = '', $attribs = null)
{
$src = $this->view->baseUrl($src);
if (is_array($attribs)) {
$attribs = $this->_htmlAttribs($attribs);
} else {
$attribs = empty($attribs) ? '' : ' '.$attribs;
}
$alt = $this->view->escape($alt);
$xhtml = '<img '
. 'src="'.$src.'" '
. 'alt="'.$alt.'"'
. $attribs
. $this->getClosingBracket();
return $xhtml;
}
}
Use cases:
echo $this->anchor('/mycontroller/myaction');
// output: /mycontroller/myaction
echo $this->anchor('/mycontroller/myaction', 'My anchor content', 'rel="nofollow"');
// output: My anchor content
echo $this->anchor('/mycontroller/myaction', 'My anchor content', 'rel="nofollow"');
// output: My anchor content
// when baseUrl is http://mydomain.com
echo $this->anchor(array(array('controller' => 'mycontroller', 'action' => 'myaction'), 'myroute'), 'My anchor content', array('rel' => 'nofollow'));
// output: My anchor content
echo $this->anchor('/mycontroller/myaction', array('src' => '/uploads/myimag.png'));
// output: <img src="/uploads/myimag.png" alt="">
// when you have an html doctype
echo $this->anchor('/mycontroller/myaction', array('src' => '/uploads/myimag.png', 'alt'=>'My alt text', array('width' => '100')));
// output: <img src="/uploads/myimag.png" alt="My alt text" width="100" />
// when you have an xhtml doctype
Well, Zend's url helper kind of thing kinda sucks. This is the only thing that pains me while developing apps in zend. In Codeigniter url helper used to come very handy. Zend has very limited resources in case of this. I had to port CI's url helper to use in my Zend Apps. And moreover, Symfony doesn't have that many helper methods like CI has and I'm not sure why.
No, you have to make one.

Categories