public function actionWeiboPost()
 {
     if ($this->isPost()) {
         $request = Yii::app()->getRequest();
         $photo = new Photo();
         $photo->weibo_id = $request->getPost('weibo_id');
         $photo->url = $request->getPost('url');
         $photo->screen_name = $request->getPost('screen_name');
         $photo->gender = $request->getPost('gender');
         $photo->location = $request->getPost('location');
         $photo->sns_uid = $request->getPost('sns_uid');
         $photo->avatar = $request->getPost('avatar');
         $photo->content = $request->getPost('content');
         $photoUpload = CUploadedFile::getInstanceByName("image");
         if ($photoUpload) {
             $mime = $photoUpload->getType();
             $allowMime = array("image/gif", "image/png", "image/jpeg", "image/jpg");
             if (!in_array($mime, $allowMime)) {
                 return $this->responseError("photo's media type is not allowed");
             } else {
                 $photo->image = savePostImage($photoUpload, $photo->weibo_id);
                 if ($photo->validate()) {
                     $photo->save();
                     return $this->responseJSON($photo, 'success');
                 }
             }
         } else {
             return $this->responseError("not get photo");
         }
     } else {
         return $this->responseError("not login");
     }
 }
function insertPostToDatabase($rs, $item_permalink, $date, $item_title, $item_content, $item_tags, $item_image, $print, $feed_id)
{
    global $log, $core;
    # Date
    if (!$date) {
        $item_date = date('Y-m-d H:i:s', time());
    } else {
        $item_date = date('Y-m-d H:i:s', $date);
    }
    # Check if item is already in the database
    $sql = "SELECT\r\n\t\t\tpost_id,\r\n\t\t\tuser_id,\r\n\t\t\tpost_title,\r\n\t\t\tpost_content,\r\n\t\t\tpost_pubdate,\r\n\t\t\tpost_image\r\n\t\tFROM " . $core->prefix . "post\r\n\t\tWHERE post_permalink = '" . $core->con->escape($item_permalink) . "'";
    $rs2 = $core->con->select($sql);
    # There is no such permalink, we can insert the new item
    if ($rs2->count() == 0 && $date < time()) {
        # Check if item is already in the database by title and by user
        $sql = "SELECT\r\n\t\t\t\tuser_id,\r\n\t\t\t\tpost_title,\r\n\t\t\t\tpost_content,\r\n\t\t\t\tpost_pubdate\r\n\t\t\tFROM " . $core->prefix . "post\r\n\t\t\tWHERE user_id = '" . $rs->user_id . "'\r\n\t\t\t\tAND post_title = '" . $item_title . "'\r\n\t\t\t\tAND post_pubdate = '" . $item_date . "'";
        $rs4 = $core->con->select($sql);
        if ($rs4->count() == 0) {
            # Get ID
            $rs3 = $core->con->select('SELECT MAX(post_id) ' . 'FROM ' . $core->prefix . 'post ');
            $next_post_id = (int) $rs3->f(0) + 1;
            $image_url = savePostImage($next_post_id, $item_image);
            $cur = $core->con->openCursor($core->prefix . 'post');
            $cur->post_id = $next_post_id;
            $cur->user_id = $rs->user_id;
            $cur->post_pubdate = $item_date;
            $cur->post_permalink = $core->con->escape($item_permalink);
            $cur->post_title = $item_title;
            $cur->post_content = $item_content;
            $cur->post_image = $image_url;
            $cur->post_status = $rs->feed_trust == 1 ? 1 : 2;
            $cur->post_comment = $rs->feed_comment;
            $cur->created = array(' NOW() ');
            $cur->modified = array(' NOW() ');
            $cur->insert();
            foreach ($item_tags as $tag) {
                $cur2 = $core->con->openCursor($core->prefix . 'post_tag');
                $cur2->post_id = $next_post_id;
                $cur2->user_id = $rs->user_id;
                $cur2->tag_id = $tag;
                $cur2->insert();
            }
            postNewsOnSocialNetwork($item_title, $rs->user_fullname, $next_post_id);
            checkSharedLinkCount($next_post_id);
            return logMsg("Post added: " . $item_permalink, "", 1, $print);
        } elseif ($rs4->count() == 1) {
            # Update post permalink in database
            $cur = $core->con->openCursor($core->prefix . 'post');
            $cur->post_permalink = $core->con->escape($item_permalink);
            $cur->modified = array('NOW()');
            $cur->update("WHERE " . $core->prefix . "post.user_id = '" . $rs->user_id . "'\r\n\t\t\t\tAND " . $core->prefix . "post.post_title = '" . $item_title . "'");
            # On informe que tout est ok
            return logMsg("Permalink updated : " . $item_permalink, "", 1, $print);
        } else {
            return logMsg("Several posts from the same author have the same title but not the same permalink : " . $item_permalink . " (Do not know it we need to update or to add the idem)", "", 3, $print);
        }
    } elseif ($rs2->count() == 1) {
        $title2 = $rs2->f('post_title');
        $content2 = $rs2->f('post_content');
        $post_id = $rs2->f('post_id');
        $user_id = $rs2->f('user_id');
        # Update tags if needed
        $old_tags = array();
        $tagRq = $core->con->select('SELECT tag_id, user_id FROM ' . $core->prefix . 'post_tag WHERE post_id = ' . $post_id);
        $tags_to_append = $item_tags;
        # par defaut TOUT
        $tags_to_remove = array();
        # par defaut RIEN
        while ($tagRq->fetch()) {
            # Si le tag existe deja, ne pas l'ajouter
            $rm_i = -1;
            foreach ($tags_to_append as $key => $value) {
                if ($value == $tagRq->tag_id) {
                    $rm_i = $key;
                }
            }
            if ($rm_i >= 0) {
                unset($tags_to_append[$rm_i]);
            }
            /*			if (in_array($tagRq->tag_id, $item_tags)) {
            				$key = array_search($tagRq, $item_tags);
            				unset($tags_to_append[$key]);
            			}**/
            # Si le tag n'exitse plus, le supprimer
            if (!in_array($tagRq->tag_id, $item_tags) && $tagRq->user_id == 'root') {
                $tags_to_remove[] = $tagRq->tag_id;
            }
            $old_tags[] = $tagRq->tag_id;
        }
        //$tags_to_remove = array_diff($old_tags, $item_tags);
        //$tags_to_append = array_diff($item_tags, $old_tags);
        if (count($tags_to_remove) > 0) {
            foreach ($tags_to_remove as $tag) {
                $core->con->execute("DELETE FROM " . $core->prefix . "post_tag\r\n\t\t\t\t\tWHERE tag_id ='" . $core->con->escape($tag) . "' AND post_id = " . $post_id);
            }
        }
        if (count($tags_to_append) > 0) {
            foreach ($tags_to_append as $tag) {
                //				$tag = $core->con->escape($tag);
                $cur = $core->con->openCursor($core->prefix . 'post_tag');
                $cur->tag_id = $tag;
                $cur->post_id = $post_id;
                $cur->user_id = 'root';
                $cur->created = array(' NOW() ');
                try {
                    $cur->insert();
                } catch (Exception $e) {
                    print "<br>New tags :";
                    print_r($item_tags);
                    print "<br>Old tags :";
                    print_r($old_tags);
                    print "<br>to remove :";
                    print_r($tags_to_remove);
                    print "<br>to append:";
                    print_r($tags_to_append);
                    print "<br>post_id:" . $post_id . "<p>";
                    print $e;
                    exit;
                }
            }
        }
        # Si l'article a ete modifie (soit la date, soit le titre, soit le contenu)
        if ($item_date != $rs2->f('post_pubdate') && !empty($date)) {
            # Update post in database
            $cur = $core->con->openCursor($core->prefix . 'post');
            $cur->post_pubdate = $item_date;
            $cur->modified = array('NOW()');
            $cur->update("WHERE " . $core->prefix . "post.post_permalink = '" . $core->con->escape($item_permalink) . "'");
            # On informe que tout est ok
            return logMsg("Date updated: " . $item_permalink, "", 1, $print);
        }
        if (!empty($item_title) && strcmp($item_title, $title2) != 0 || !empty($item_content) && strcmp($item_content, $content2) != 0) {
            # Update post in database
            if (strcmp($item_title, $title2) != 0) {
                $cur = $core->con->openCursor($core->prefix . 'post');
                $cur->modified = array('NOW()');
                $cur->post_title = $item_title;
                $cur->update("WHERE " . $core->prefix . "post.post_permalink = '" . $core->con->escape($item_permalink) . "'");
                $log_msg = logMsg(T_("Title change of : ") . $item_permalink, "", 2, $print);
                if ($log == "debug") {
                    $log_msg .= logMsg("Old : " . $title2, "", 4, $print);
                    $log_msg .= logMsg("New : " . $item_title, "", 4, $print);
                }
            }
            if (strcmp($item_content, $content2) != 0) {
                $image_url = savePostImage($rs2->f('post_id'), $item_image);
                $cur = $core->con->openCursor($core->prefix . 'post');
                $cur->modified = array('NOW()');
                $cur->post_content = $item_content;
                $cur->post_image = $image_url;
                $cur->update("WHERE " . $core->prefix . "post.post_permalink = '" . $core->con->escape($item_permalink) . "'");
                $log_msg = logMsg(T_("Content change of : ") . $item_permalink, "", 2, $print);
                if ($log == "debug") {
                    $log_msg .= logMsg("Old : " . $content2, "", 4, $print);
                    $log_msg .= logMsg("New : " . $item_content, "", 4, $print);
                }
            }
            return $log_msg;
        }
        # fin du if($date !=
    }
    return "";
}