File: /var/www/html/wp-content/plugins/ganryu_shop/model/Share.php
<?php
require_once dirname(__FILE__).'/Abstract.php';
class ganryu_shop_model_Share extends ganryu_shop_model_Abstract{
function getShareCode($userid, $articletype, $articleid, $snss){
if (!is_array($snss)){
$snss = array($snss);
}
if (!$snss){
return array();
}
$w = "";
$b = array();
$pidx = 0;
foreach($snss as $sns){
if ($w) $w.=",";
$w .= ":sns$pidx";
$b["sns$pidx"] = $sns;
$pidx++;
}
$tbl = $this->tablename('share_sns');
$sql = "
select
sns,
sharecode
from
$tbl
where
userid = :userid
and
articletype = :articletype
and
articleid = :articleid
and
sns in ($w)
and
valid = 1
";
$bind = array_merge($b, array(
'userid'=>$userid,
'articletype'=>$articletype,
'articleid'=>$articleid
));
$res = $this->selectAll($sql, $bind);
if ($res === false){
return false;
}
$resdt = array();
foreach ($res as $rd){
$resdt[$rd['sns']] = $rd['sharecode'];
}
return $resdt;
}
function createShareCode($userid, $articletype, $articleid, $snss){
if (!is_array($snss)){
$snss = array($snss);
}
$tbl = $this->tablename('share_sns');
$retdt = array();
foreach ($snss as $sns){
$sql = "
select
shareid,
sharecode,
valid
from
$tbl
where
userid = :userid
and
articletype = :articletype
and
articleid = :articleid
and
sns = :sns
";
$bind = array(
'userid'=>$userid,
'articletype'=>$articletype,
'articleid'=>$articleid,
'sns'=>$sns
);
$res = $this->selectRow($sql, $bind);
if ($res === false){
return false;
}
$sharecode = grys_generate_session(8);
if (!$res){
$sql = "
insert into $tbl(
userid,
articletype,
articleid,
sns,
sharecode,
valid
)
values(
:userid,
:articletype,
:articleid,
:sns,
:sharecode,
1
)
";
$bind = array(
'userid'=>$userid,
'articletype'=>$articletype,
'articleid'=>$articleid,
'sns'=>$sns,
'sharecode'=>$sharecode
);
$res = $this->query($sql, $bind);
if (!$res){
return false;
}
$retdt[$sns] = $sharecode;
}
else{
if ($res['valid'] == 1){
$retdt[$sns] = $res['sharecode'];
}
else{
$sql = "
update $tbl set
sharecode = :sharecode,
valid = 1
from
shareid = :shareid
";
$bind = array(
'sharecode'=>$sharecode,
'shareid'=>$shareid
);
$res = $this->query($sql, $bind);
if (!$res){
return false;
}
$retdt[$sns] = $sharecode;
}
}
}
return $retdt;
}
function getShareFromCode($code){
$tbl = $this->tablename('share_sns');
$sql = "
select
shareid,
userid,
articletype,
articleid,
sns,
sharecode
from
$tbl
where
sharecode = :code
and
valid = 1
";
$bind = array('code'=>$code);
$res = $this->selectRow($sql, $bind);
if ($res === false){
return false;
}
return $res;
}
function addLog($logdata){
$tbl = $this->tablename('share_sns_log');
$sql = "
insert into $tbl(
shareid,
userid,
uniq_session,
create_date_day,
create_date,
ip,
user_agent,
referer
)
values(
:shareid,
:userid,
:uniq_session,
now(),
now(),
:ip,
:user_agent,
:referer
)
";
$bind = array(
'shareid'=>$logdata['shareid'],
'userid'=>$logdata['userid'],
'uniq_session'=>$logdata['uniq_session'],
'ip'=>$_SERVER['REMOTE_ADDR'],
'user_agent'=>$_SERVER['HTTP_USER_AGENT'],
'referer'=>$logdata['referer']
);
$res = $this->query($sql, $bind);
if (!$res){
return false;
}
return true;
}
}