itora web

22Mar/120

Moving from WordPress to jekyll

I'm moving my website from worpdress to jekyll.
I'm moving the hosting too, because github is free.
And I'm changing domain name.
Yes, spring is coming, time to change.
So, if you wanna read what I'm writing, you can follow me here edapx.com.
Ah, the new feed is available here (there is a noisy screensaver that I will remove)

Filed under: Uncategorized No Comments
12Mar/120

cakePHP youtube datasource

I've seen in google analytics that mosts visitors of this blog speak english. I've thought that the italians can understand what i'm writing, so, here we are, i've switched language.
I've just pushed on github a cakePHP youtube datasource, to retrieve information (all the metatags) regarding a video. It's still incomplete, I would like to add some others functionality, like retrieve the standard feed for a given category, or retrieve a video with more than X views. Ecc, anyway, Fork Me.
Here I provide a solution that I've adopted to save in the database the fields "video_title", "video_thumb" and "video_id" for a given youtube video link associated to a model, for example an Article model containing a video, or a Submission Model containing a youtube video for a video contest. This code validates the video URL and, if valid, add the videos metatags to the data model to save, in this example, the Article model.
1)Follow the instruction on the github page and install the datasource
2) Add this rule to your $validate array in the model where you would like to save the video information

'video' => array(
    'rule' => 'validYoutube',
    'message' => 'the given videos link is incorrect',
    'allowEmpty' => true,
),

3)Add this functions in the Model, that checks if the video is valid, and add the metatags to the $data array

<?
    public function validYoutube(array $data ){
        $url = current($data);
        $video_info = $this->getVideo($url);
        if ($video_info) {
            $this->setVideoValues($video_info);
            return true;
        }else{
            return false;
        }
    }

    public function getVideo($id){
	$this->Youtube = ConnectionManager::getDataSource('Youtube');
        $video = $this->Youtube->findById($id);
        if (!$video) {
            return false;
        }
        $info = $this->Youtube->formatData($video);
        return $info;
    }

    public function setVideoValues($info){
        if (empty($info)) {
            return false;
        }
        $this->data['Article']['video'] = $info['id'];
        $this->data['Article']['video_thumb'] = $info['default_thumb'];
	$this->data['Article']['video_title'] = $info['title'];
    }
?>

Thanks debuggable for this article on datasources in CakePHP

3Mar/120

CakePHP 2.0 Comments Spam Datasource

Ho trovato questo post che spiega come implementare akismet per verificare i commenti sul nostro sito.Mi e' sembrata davvero la soluzione piu' semplice fra le tante disponibili. I commenti vengono salvati nel nostro database, e dopo il salvataggio verifichiamo tramite akismet se il commento e' spam o meno. Ecco qui i passaggi:
1) Scaricarsi il datasource akismet.php ed metterlo in app/Model/Datasource.
1)Creare una api key sul sito di akisemt, ed aggiungere queste linee al file database.php

public $Akismet = array(
    'datasource' => 'Akismet',
    'blog' => 'http://yourblog/',
    'key' => 'your_akismet_key'
);

2)Nella nostra tabella destinanata ai commenti, aggiungere i campi spam e checked
3)Aggiungere al model Comment.php la seguente funzione.

public function checkForSpam($id) {
	$comment = $this->read(null, $id);
	App::uses('HttpSocket', 'Network/Http');
	$this->Http = new HttpSocket();
	$this->Akismet = ConnectionManager::getDataSource('Akismet');
	$data = array(
	      'user_ip' => $comment['Comment']['ip'],
	      'user_agent' => $_SERVER['HTTP_USER_AGENT'],
	      'referrer' => 'http://www.google.com',
	      'permalink' => '/comments/view/'.$comment['Comment']['id'],
	      'comment_type' => 'comment',
	      'comment_author' => $comment['Comment']['name'],
	      'comment_author_email' => $comment['Comment']['email'],
	      'comment_author_url' => '',
	      'comment_content' => $comment['Comment']['text']
		);
	$check = $this->Akismet->checkSpam($data);
	if($check == 'true') {
		$spam = array(
			'id' => $comment['Comment']['id'],
			'checked' => 1,
			'spam' => 1
		);
	} else {
		$spam = array(
			'id' => $comment['Comment']['id'],
			'checked' => 1,
			'spam' => 0
		);
	};
	return $this->save($spam, false);
}

4) Nel vostro CommentsController file, dopo aver salvato il commento richiamate la funzione appena creata. Es

public function add() {
	if ($this->request->is('post')) {
		$this->Comment->create();
		if ($this->Comment->save($this->request->data)) {
			$this->Comment->checkForSpam($this->Comment->getInsertID());
			$this->Session->setFlash(__('The comment has been saved'));
			$this->redirect(array('action' => 'index'));
		} else {
			$this->Session->setFlash(__('The comment could not be saved. Please, try again.'));
		}
	}
	$articles = $this->Comment->Article->find('list');
	$this->set(compact('articles'));
}

5)Nella vostra sezione amministrativa, elencate tutti i commenti, e verificate i falsi positivi, in caso ce ne siano
6)Sul vostro sito, mostrate solo i commmenti con il valore di "spam" = 0

Tagged as: , No Comments