はるばるITつれづれ人生

日々の出来ことエトセトラ

ITに関係ないサラリーマンがコミュニティサイト作っちゃいました。

私ごとですが、この度コミュニティサイトを作成いたしました。
私の持てる知識を絞り出してなんとか完成しました。

f:id:asufaruto2009:20150824171236j:plain

 私は普段webとは関係ない仕事してます。なんとか休日を利用して、
webサービス作成しました。


メインのコードはこんな感じ

----------------------------------------------------------------------------------------------------------------------

<?php
App::uses('Sanitize', 'Utility');
class RssfeedsController extends AppController {

public $helpers = array('Html','Form','Session','Number','Cache');

public $uses = array(
'Rssfeed',
'Tweet',
'Wadai',
'Rank',
'Oracle'
);
//***************************************************************************************************
public function index() {

$title_for_layout = "LapyNetニュース";
$content = "Hello World!!";

$this->set(compact('title_for_layout','content'));
if(date("G")>0 and date("G") < 7){
$today = date("Y-m-d", strtotime('-1 day'));
}else{
$today = date("Y-m-d");
}

$params = array(
'conditions' => array(
'and' => array(
'Rssfeed.time' => $today,
)),
'order' => 'tweets desc',
'limit' => '50'
);
$this->set('posts',$this->Rssfeed->find('all',$params));

$paramz = array(
'order' => 'Rssfeed.id desc',
'limit' => '10'
);
$this->set('posts2',$this->Rssfeed->find('all',$paramz));

$paramz = $this->Rssfeed->query('SELECT id,title FROM rssfeeds order by id desc limit 10');

$this->set('posts2', $paramz);

$parapara = $this->Oracle->query('SELECT id,title FROM oracles order by id desc limit 10');

$this->set('postx', $parapara);

$funking = $this->Rank->query('SELECT id,frank FROM ranks where creation=(select max(creation) from ranks)');

$this->set('funking', $funking);


}

//***************************************************************************************************
public function mizuno() {
$parapara = array(
'order' => 'Oracle.id desc',
'limit' => '30'
);
$recent_posts = $this->Oracle->find('all',$parapara);
return $recent_posts;

}


//***************************************************************************************************
public function wadai() {
if(date("G")>0 and date("G") < 7){
$today = date("Y-m-d", strtotime('-1 day'));
}else{
$today = date("Y-m-d");
}

$params = array(
'conditions' => array(
'and' => array(
'Rssfeed.time' => $today,
'Rssfeed.kubetu' => 4
)),
'order' => 'tweets desc',
'limit' => '100'
);
$this->set('posts',$this->Rssfeed->find('all',$params));





$this->set("title_for_layout","2CHニュース-LapyNetニュース");


$paramz = array(
'order' => 'Rssfeed.id desc',
'limit' => '10'
);
$this->set('postyy',$this->Rssfeed->find('all',$paramz));

$parapara = array(
'order' => 'Oracle.id desc',
'limit' => '10'
);


$this->set('postyx',$this->Oracle->find('all',$parapara));


$funking = $this->Rank->query('SELECT * FROM ranks where creation=(select max(creation) from ranks)');

$this->set('funking', $funking);



}
//***************************************************************************************************
public function hot() {
if(date("G")>0 and date("G") < 7){
$today = date("Y-m-d", strtotime('-1 day'));
}else{
$today = date("Y-m-d");
}

$params = array(
'conditions' => array(
'and' => array(
'Rssfeed.time' => $today,
'Rssfeed.kubetu' => 3
)),
'order' => 'tweets desc',
'limit' => '20'
);
$title_for_layout = "LapyNetニュース";
$this->set("title_for_layout","ニュース-LapyNetニュース");
$this->set('posts',$this->Rssfeed->find('all',$params));

$parapara = array(
'order' => 'Oracle.id desc',
'limit' => '10'
);
$this->set('postxx',$this->Oracle->find('all',$parapara));

$paramz = array(
'order' => 'Rssfeed.id desc',
'limit' => '10'
);
$this->set('postsxxx',$this->Rssfeed->find('all',$paramz));

$funking = $this->Rank->query('SELECT * FROM ranks where creation=(select max(creation) from ranks)');

$this->set('funking', $funking);


}


//***************************************************************************************************
public function newnew() {
if(date("G")>0 and date("G") < 7){
$today = date("Y-m-d", strtotime('-1 day'));
}else{
$today = date("Y-m-d");
}

$params = array(
'conditions' => array(
'and' => array(
'Rssfeed.time' => $today,
'Rssfeed.kubetu' => 2
)),
'order' => 'tweets desc',
'limit' => '20'
);
$this->set("title_for_layout","エンタメニュース-LapyNetニュース");
$this->set('posts',$this->Rssfeed->find('all',$params));

$paramz = array(
'order' => 'Rssfeed.id desc',
'limit' => '10'
);
$this->set('postszz',$this->Rssfeed->find('all',$paramz));

$paramz = $this->Oracle->query('SELECT id,title FROM rssfeeds order by id desc limit 10');

$this->set('postszz', $paramz);


$parapara = $this->Oracle->query('SELECT id,title FROM oracles order by id desc limit 10');

$this->set('postzy', $parapara);


$funking = $this->Rank->query('SELECT * FROM ranks where creation=(select max(creation) from ranks)');

$this->set('funking', $funking);



}
//***************************************************************************************************
public function top() {
if(date("G")>0 and date("G") < 7){
$today = date("Y-m-d", strtotime('-1 day'));
}else{
$today = date("Y-m-d");
}
$params = array(
'conditions' => array(
'and' => array(
'Rssfeed.time' => $today,
'Rssfeed.kubetu' => 1
)),
'order' => 'tweets desc',
'limit' => '20'
);
$this->set('posts',$this->Rssfeed->find('all',$params));

$param = array(
'conditions' => array('times > current_timestamp + interval -600 minute and Kubetu = 4'),
'order' => 'tweets desc',
'limit' => '30'
);
$this->set('posts2',$this->Rssfeed->find('all',$param));
}


//***************************************************************************************************
public function view($id = null ) {
$this->Rssfeed->id = $id; //驥崎ヲ√↑荳譁・i縺励>
$user = AuthComponent::user();

$TwimStatus = ClassRegistry::init('Twim.TwimStatus');


$TwimStatus->getDataSource()->setToken(AuthComponent::user());

$statuses = $TwimStatus->tweet();
$rairai = $this->Rssfeed->read(title,$id);
$alcol = $this->Rssfeed->read();
$this->set('post', $alcol);

$this->set('post2', $user);
$this->set('post3', $statuses);
$this->set("title_for_layout",$rairai[Rssfeed][title]."-LapyNetニュース");

$paramz = array(
'order' => 'Rssfeed.id desc',
'limit' => '20'
);
$this->set('postszz',$this->Rssfeed->find('all',$paramz));

$parapara = array(
'order' => 'Oracle.id desc',
'limit' => '30'
);
$this->set('postzy',$this->Oracle->find('all',$parapara));

}
//***************************************************************************************************
public function add() {

if($this->request->isPost()){
if($this->Tweet->save($this->data)) {
$this->Session->setFlash('seikou');
$this->redirect(array('controller'=>'rssfeeds','action'=>'view',$contents11));

}else{
$this->Session->setFlash('sipai');
$this->redirect(array('controller'=>'rssfeeds','action'=>'view',$contents11));
}
}
}
//***************************************************************************************************
public function delete($id = null) {
$this->Rssfeed->id = $id;
$this->set('set', $this->Rssfeed->read());
$this->redirect(array('controller'=>'tweets','action'=>'delete',$this->data['tweet']['rssfeed_id']));


}
//***************************************************************************************************
public function oracle() {
if($this->request->is('post')) {

$user = AuthComponent::user();

$TwimStatus = ClassRegistry::init('Twim.TwimStatus');


$TwimStatus->getDataSource()->setToken(AuthComponent::user());

$contents1 = $this->request->data['Rssfeed']['contents'];
$contents2 = $this->request->data;
$statuses = $TwimStatus->tweet($contents1);


if($this->Rssfeed->save($contents2)) {
$this->Session->setFlash('成功です。');
$this->redirect(array('controller'=>'rssfeeds','action'=>'view',$contents11));
$this->redirect($this->referer());
}else{
$this->redirect(array('controller'=>'rssfeeds','action'=>'view',$contents11));
$this->Session->setFlash('失敗です。');
}
}


}
//***************************************************************************************************
public function makelist() {
$rssfeeds = $this->paginate();
if ($this->request->is('requested')) {
return $rssfeeds;
} else {
$this->set('rssfeeds', $rssfeeds);
}
}

}

----------------------------------------------------------------------------------------------------------------------

rssフィードはこんな感じです。
----------------------------------------------------------------------------------------------------------------------

#!/usr/bin/php
<?php

header('Content-type: text/html; charset=UTF-8');


require_once ('/var/www/html/s/magpierss/rss_fetch.inc');


define('MAGPIE_CACHE_AGE', 60*5);

define('MAGPIE_CACHE_DIR', '/var/www/html/s/cache/');

define('MAGPIE_OUTPUT_ENCODING','UTF-8');


$urls = array(
'*********************************');

$items = getRssList($urls);

echo '<ul>';
foreach ($items as $item) {

$href = mb_convert_encoding($item['link'], "UTF-8", "auto");

$title = mb_convert_encoding($item['title'], "UTF-8", "auto");
$description = mb_convert_encoding($item['description'], "UTF-8", "auto");
$megami = mb_convert_encoding($description, "UTF-8", "auto");

 

//ツイート数カウント用******************************************************************************************************


$all = 'http://urls.api.twitter.com/1/urls/count.json?url='.$href;
$Rits = file_get_contents($all);
$Olfe = mb_convert_encoding($Rits, 'UTF-8', 'auto');
$data = json_decode($Olfe);
$tweets = $data->count;

//***************************************************************************************************

$checkman = db_check_urls($href);
$checkman2 = db_check_pr($title);

$nowtimes = date('Ymd');

 


if($checkman == 0 && $checkman2 == 0){
#$hrefs = urlencode($href);
db_put_urls($nowtimes,$href,$title,$description,$tweets);
echo "<li><a href=$href>$title</a><br>$megami<p>$tweets</p><p>$checkman</p></li>";
}else{

}


}
echo '</ul>';

 

 

// 複数サイトのRSSのアイテムを取得****************************************************************
function getRssList($urls)
{
$rssitems = array();
foreach ($urls as $url) {
$obj = fetch_rss($url);
$rssitems = array_merge($rssitems, $obj->items);
}
return $rssitems;
}

//***********************************************************************************************
function db_check_pr($japanese){

$moji = mb_substr($japanese,0,2);
if($moji == 'PR'){
return 1;
}else{
return 0;
}
}
//***********************************************************************************************

function db_check_urls($href){

require '/var/www/html/s/database_conf.php';

try {

$db = new PDO($dsn, $dbUser, $dbPass);
$db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);


$sql = 'SELECT count(*) AS count FROM rssfeeds WHERE urls = :language';
$prepare = $db->prepare($sql);


$languages = array($href);
foreach ($languages as $language) {

$prepare->bindValue(':language', $language, PDO::PARAM_STR);
$prepare->execute();

$result = $prepare->fetchColumn();
if ($result > 0) {
return true;
}else{
return false;
}
}

} catch (PDOException $e) {
echo 'エラーが発生しました。内容: ' . h($e->getMessage());
}

function h($var)
{
return htmlspecialchars($var, ENT_QUOTES, 'UTF-8');
}
}

//***************************************************************************************************
function db_put_urls($nowtimes,$href,$title,$description,$tweets){
require '/var/www/html/s/database_conf.php';
try {

$db = new PDO($dsn, $dbUser, $dbPass);
$db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sikibetu = 5;
$moto = "アニゲー速報VIP";

$sql = 'insert into rssfeeds (id, time,urls,title,contents,tweets,times,kubetu,teikyo) values (?, ?, ?, ?, ?, ?, ?, ?, ?)';

$prepare = $db->prepare($sql);

$prepare->bindValue(1, 0, PDO::PARAM_STR);
$prepare->bindValue(2, $nowtimes, PDO::PARAM_STR);
$prepare->bindValue(3, $href, PDO::PARAM_STR);
$prepare->bindValue(4, $title, PDO::PARAM_STR);
$prepare->bindValue(5, $description, PDO::PARAM_STR);
$prepare->bindValue(6, $tweets, PDO::PARAM_STR);
$prepare->bindValue(7, 'cast( now() as datetime)', PDO::PARAM_STR);
$prepare->bindValue(8, $sikibetu, PDO::PARAM_STR);
$prepare->bindValue(9, $moto, PDO::PARAM_STR);
$prepare->execute();

} catch (PDOException $e) {
echo 'エラーが発生しました。内容: ' . h($e->getMessage());
}
}
?>

---------------------------------------------------------------------------------------------------------------------
完成したサイトがこちらです。

LapyNetニュース

http://lapynetz.net/

 参考にした書籍

CakePHP2 実践入門 (WEB+DB PRESS plus)

CakePHP2 実践入門 (WEB+DB PRESS plus)

 
Webアプリ開発を加速する CakePHP2定番レシピ119

Webアプリ開発を加速する CakePHP2定番レシピ119

 

 一番難しいところはtwitterログインとwebサイト連結でした。

cakephpは直感できに操作できていいです。
素人のわたしでも操作できましたので。

cakephpを使用しています。
webからニュースを収集してきます。
ツイート数でランキングしてます、ネットの話題を見つけられます。

twitterアカウントでログインできます。

ニュースに対するコメントもできるし、
自分の記事も投稿できます。(画像も可)

デザインはよくわからないので、
ホームページビルダーを使用しています。
今のホームページビルダーは昔と違って高機能ですね。
テンプレートも何種類もあって今はすごいです。

 

アマゾンのawsを使用してます。
サーバーはWebサーバーとデータベースサーバ(mysql)
に分かれています。
今更cakephpを使っている人は少ないかもしれませんが、
データベースとの接続などは楽でした。

twitterアカウントあるといつでも参加できますので、
ニュースを通してコミュニケーションを目的としたサイトを目指します。