PageRenderTime 60ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/core/classes/log.class.php

http://github.com/newscloud/open-social-media-toolkit
PHP | 647 lines | 495 code | 62 blank | 90 comment | 43 complexity | 215c15f5f7f7810f2caf95d32dc72ad7 MD5 | raw file
  1. <?php
  2. /*
  3. *
  4. * // Create the Log table
  5. $manageObj->addTable("Log","id","BIGINT(20) unsigned NOT NULL auto_increment","MyISAM");
  6. *
  7. */
  8. require_once(PATH_CORE .'/classes/dbRowObject.class.php');
  9. class LogRow extends dbRowObject
  10. {
  11. function __construct( $db, $table, $fields, $idname='id' )
  12. {
  13. parent::__construct($db, $table, $fields, $idname);
  14. $this->filterFieldFromInsertAndUpdate('t'); // prevent timestamp field from being stomped on
  15. }
  16. function insert()
  17. {
  18. // hack: exclude the t field because its a timestamp. bit of a hack
  19. //dbRowObject::$debug = true;
  20. // todo: make this work
  21. //unset($this->nonidfields['t']);
  22. //echo '<pre>'.print_r($this->nonidfields, true) . '</pre>';
  23. parent::insert();
  24. //$this->nonidfields['t']='';
  25. }
  26. }
  27. class LogTable
  28. {
  29. ///////////////////////////////////////////////////////////////////////////////////////////////////////
  30. // standard table fields
  31. var $db;
  32. static $tablename="Log";
  33. static $idname = "id";
  34. static $idtype = "BIGINT(20) unsigned NOT NULL auto_increment";
  35. static $dbRowObjectClass = "LogRow";
  36. static $fields = array(
  37. "userid1" => "BIGINT(20)default 0",
  38. "action" => // IMPORTANT NOTE: see mysql docs about ALTER ... MODIFY and enums before attempting to change the spelling of any fields. you will corrupt the existing data!
  39. "ENUM('vote','comment',
  40. 'readStory','readWire','invite','postStory','publishWire',
  41. 'publishStory','shareStory','referReader','referToSite',
  42. 'postTwitter', 'signup', 'acceptedInvite',
  43. 'redeemed', 'wonPrize', 'completedChallenge', 'addedWidget', 'addedFeedHeadlines',
  44. 'friendSignup', 'addBookmarkTool', 'levelIncrease','sessionsRecent','sessionsHour','pageAdd','chatStory','postBlog','sendCard','askQuestion','answerQuestion','likeQuestion','likeAnswer','likeIdea','likeStuff','addStuff','storyFeatured','madePredict'
  45. ) default 'readStory'",
  46. "itemid" => "INT(11) default 0",
  47. "itemid2" => "INT(11) default 0",
  48. "userid2" => "BIGINT(20) default 0",
  49. "ncUid" => "BIGINT(20) default 0",
  50. "t" => "timestamp", // DEFAULT CURRENT_TIMESTAMP", // back to what it was - default current AND on update
  51. "dateCreated" => "DATETIME", // a timestamp to record creation, but i'm not even going to try to let mysql handle it automatically
  52. "status" => "ENUM('pending','ok','error') default 'pending'",
  53. "isFeedPublished" => "ENUM('pending','complete') default 'pending'"
  54. );
  55. static $keydefinitions = array();
  56. ///////////////////////////////////////////////////////////////////////////////////////////////////////
  57. // standard table functions
  58. function __construct(&$db=NULL)
  59. {
  60. if (is_null($db))
  61. {
  62. require_once('db.class.php');
  63. $this->db=new cloudDatabase();
  64. } else
  65. $this->db=$db;
  66. }
  67. // although many functions will be duplicated between table subclasses, having a parent class gets too messy
  68. function getRowObject()
  69. {
  70. $classname = self::$dbRowObjectClass;
  71. return new $classname($this->db, self::$tablename, array_keys(self::$fields), self::$idname);
  72. }
  73. // generic table creation routine, same for all *Table classes
  74. static function createTable($manageObj)
  75. {
  76. $manageObj->addTable(self::$tablename,self::$idname,self::$idtype,"MyISAM");
  77. foreach (array_keys(self::$fields) as $key)
  78. {
  79. $manageObj->updateAddColumn(self::$tablename,$key,self::$fields[$key]);
  80. }
  81. foreach (self::$keydefinitions as $keydef)
  82. {
  83. $manageObj->updateAddKey(self::$tablename,$keydef[0], $keydef[1], $keydef[2], $keydef[3]);
  84. }
  85. }
  86. ///////////////////////////////////////////////////////////////////////////////////////////////////////
  87. function testPopulate()
  88. {
  89. echo 'LogTable->testPopulate()<br>';
  90. $l = $this->getRowObject();
  91. $l->action='comment';
  92. $l->userid1=666;
  93. $l->insert();
  94. }
  95. }
  96. class LogExtraRow extends dbRowObject
  97. {
  98. }
  99. class LogExtraTable
  100. {
  101. ///////////////////////////////////////////////////////////////////////////////////////////////////////
  102. // standard table fields
  103. var $db;
  104. static $tablename="LogExtra";
  105. static $idname = "id";
  106. static $idtype = "BIGINT(20) unsigned NOT NULL auto_increment";
  107. static $dbRowObjectClass = "LogExtraRow";
  108. static $fields = array(
  109. "logid" => "BIGINT(20)default 0",
  110. "txt" => "TEXT default ''"
  111. );
  112. static $keydefinitions = array();
  113. ///////////////////////////////////////////////////////////////////////////////////////////////////////
  114. // standard table functions
  115. function __construct(&$db=NULL)
  116. {
  117. if (is_null($db))
  118. {
  119. require_once('db.class.php');
  120. $this->db=new cloudDatabase();
  121. } else
  122. $this->db=$db;
  123. }
  124. // although many functions will be duplicated between table subclasses, having a parent class gets too messy
  125. function getRowObject()
  126. {
  127. $classname = self::$dbRowObjectClass;
  128. return new $classname($this->db, self::$tablename, array_keys(self::$fields), self::$idname);
  129. }
  130. // generic table creation routine, same for all *Table classes
  131. static function createTable($manageObj)
  132. {
  133. $manageObj->addTable(self::$tablename,self::$idname,self::$idtype,"MyISAM");
  134. foreach (array_keys(self::$fields) as $key)
  135. {
  136. $manageObj->updateAddColumn(self::$tablename,$key,self::$fields[$key]);
  137. }
  138. foreach (self::$keydefinitions as $keydef)
  139. {
  140. $manageObj->updateAddKey(self::$tablename,$keydef[0], $keydef[1], $keydef[2], $keydef[3]);
  141. }
  142. }
  143. ///////////////////////////////////////////////////////////////////////////////////////////////////////
  144. }
  145. class log {
  146. var $db; // database object
  147. function log(&$db=NULL) {
  148. if (is_null($db)) {
  149. require_once('db.class.php');
  150. $this->db=new cloudDatabase();
  151. } else
  152. $this->db=$db;
  153. }
  154. function narrate($obj='') {
  155. // writes to smtSync.log
  156. $this->db->log($obj,PATH_SYNCLOGFILE);
  157. }
  158. function fetch($id) {
  159. // return log item as a data object
  160. $this->db->query("SELECT * FROM Log WHERE id=$id;");
  161. $data=$this->db->read();
  162. return $data;
  163. }
  164. function fetchExtra($id) {
  165. // return log item as a data object
  166. $this->db->query("SELECT Log.*,LogExtra.txt FROM Log LEFT JOIN LogExtra ON Log.id=LogExtra.logid WHERE Log.id=$id;");
  167. $data=$this->db->read();
  168. return $data;
  169. }
  170. function transmit($timestamp=0,$limit=25) {
  171. require_once('utilities.class.php');
  172. $utilObj=new utilities($this->db);
  173. // return serialized log info for synchronization
  174. $log=array();
  175. /*** Transmit votes - req. ncUid and contentid ***/
  176. $votes=array();
  177. $x=0;
  178. $page=0;
  179. $q=$this->db->query("SELECT Log.*,User.ncUid,Content.contentid FROM Log,User,Content WHERE Log.itemid=Content.siteContentId AND contentid>0 AND User.ncUid<>0 AND Log.userid1=User.userid AND action='vote' AND status='pending' AND Content.isBlocked=0 AND User.isBlocked=0 ORDER BY Log.id DESC");
  180. while ($data=$this->db->readQ($q)) {
  181. $votes[$x][contentid]=$data->contentid;
  182. $votes[$x][logid]=$data->id;
  183. // fetch newscloud id from userid1
  184. $votes[$x][uid]=$data->ncUid; // $this->lookupUid($data->userid1);
  185. $x+=1;
  186. }
  187. $this->narrate('Transmitting votes');
  188. $this->narrate($votes);
  189. $log['votes']=$votes;
  190. /*** Transmit new users - req isVerifiedEmail=1 ***/
  191. $newUsers=array();
  192. $x=0;
  193. require_once('userRemoteSync.class.php');
  194. $rsObj=new userRemoteSync($this->db);
  195. require_once (PATH_CORE.'/classes/systemStatus.class.php');
  196. $ssObj=new systemStatus($this->db);
  197. $partnerid=$ssObj->getState('partnerid');
  198. $q=$rsObj->findUnlinkedAccounts();
  199. while ($data=$this->db->readQ($q)) {
  200. $newUsers[$x][partnerid]=$partnerid;
  201. $newUsers[$x][userid]=$data->userid;
  202. $newUsers[$x][name]=$data->name;
  203. $newUsers[$x][email]=$data->email;
  204. $newUsers[$x][fbId]=$data->fbId;
  205. $newUsers[$x][city]=$data->city;
  206. $x+=1;
  207. }
  208. $log['newUsers']=$newUsers;
  209. $this->narrate('New users to sync with NewsCloud');
  210. $this->narrate($newUsers);
  211. // build for users to synchronize - changed bio, changed user level - to do : facebook image
  212. /*** Transmit users who change their user level ***/
  213. $levelChanges=array();
  214. $x=0;
  215. require_once('userRemoteSync.class.php');
  216. $rsObj=new userRemoteSync($this->db);
  217. $q=$rsObj->findUserLevelIncreases($timestamp);
  218. while ($data=$this->db->readQ($q)) {
  219. $levelChanges[$x][logid]=$data->id;
  220. $levelChanges[$x][userid]=$data->userid;
  221. $levelChanges[$x][uid]=$data->ncUid;
  222. $levelChanges[$x][userlevel]=$data->itemid;
  223. $x+=1;
  224. }
  225. $log['levelChanges']=$levelChanges;
  226. $this->narrate('User Level Increases');
  227. $this->narrate($levelChanges);
  228. /*** Transmit read story records - req. ncUid and contentid ***/
  229. $q=$this->db->query("SELECT Log.id,Content.contentid,User.ncUid FROM Log,Content,User WHERE action='readStory' AND status='pending' AND User.userid=Log.userid1 AND Log.itemid=Content.siteContentId AND User.ncUid>0 AND Content.contentid>0 AND Content.isBlocked=0 AND User.isBlocked=0 ORDER BY id DESC LIMIT $limit;");
  230. $readStory=array();
  231. $x=0;
  232. while ($data=$this->db->readQ($q)) {
  233. $readStory[$x][contentid]=$data->contentid;
  234. $readStory[$x][logid]=$data->id;
  235. $readStory[$x][uid]=$data->ncUid;
  236. $x+=1;
  237. }
  238. $log['readStory']=$readStory;
  239. /*** Transmit published story to journal - req. ncUid and contentid ***/
  240. $q=$this->db->query("SELECT Log.id,Content.contentid,User.ncUid FROM Log,Content,User WHERE action='publishStory' AND status='pending' AND User.userid=Log.userid1 AND Log.itemid=Content.siteContentId AND User.ncUid>0 AND Content.contentid>0 AND Content.isBlocked=0 AND User.isBlocked=0 ORDER BY id DESC LIMIT $limit;");
  241. $pubStory=array();
  242. $x=0;
  243. while ($data=$this->db->readQ($q)) {
  244. $pubStory[$x][contentid]=$data->contentid;
  245. $pubStory[$x][logid]=$data->id;
  246. $pubStory[$x][uid]=$data->ncUid;
  247. $x+=1;
  248. }
  249. $log['pubStory']=$pubStory;
  250. /*** Transmit posted raw stories - req ncUid ***/
  251. $q=$this->db->queryC("SELECT Log.id,Log.itemid,User.ncUid FROM Log,User WHERE action='publishWire' AND status='pending' AND User.userid=Log.userid1 AND User.ncUid>0 AND User.isBlocked=0 ORDER BY id DESC LIMIT $limit;");
  252. $pubWire=array();
  253. $x=0;
  254. while ($data=$this->db->readQ($q)) {
  255. $wireQuery=$this->db->queryC("SELECT * FROM Newswire WHERE id=$data->itemid;");
  256. if ($wireQuery!==false) {
  257. $wi=$this->db->readQ($wireQuery);
  258. $pubWire[$x][logid]=$data->id;
  259. $pubWire[$x][uid]=$data->ncUid;
  260. // fetch wire story url
  261. $pubWire[$x][url]=$wi->url;
  262. $pubWire[$x][siteContentId]=$data->itemid2;
  263. $pubWire[$x][wireid]=$wi->wireid;
  264. switch ($wi->feedType) {
  265. default: // wire
  266. $pubWire[$x][feedType]='wire';
  267. break;
  268. case 'blog':
  269. $pubWire[$x][feedType]='blog';
  270. $pubWire[$x][title]=$wi->title;
  271. $pubWire[$x][date]=$wi->date;
  272. $pubWire[$x][caption]=$utilObj->shorten($wi->caption,500);
  273. break;
  274. }
  275. $x+=1;
  276. } else {
  277. // story deleted from newswire
  278. $this->db->update("Log","status='error'","id=$data->id");
  279. }
  280. }
  281. $log['pubWire']=$pubWire;
  282. /*** Transmit posted stories, a new user posted story - req ncUid ***/
  283. $q=$this->db->queryC("SELECT Log.id,Log.itemid,User.ncUid FROM Log,User WHERE action='postStory' AND status='pending' AND User.userid=Log.userid1 AND User.ncUid>0 AND User.isBlocked=0 ORDER BY id DESC LIMIT $limit;");
  284. if ($q!==false) {
  285. //$this->db->log('inside poststory');
  286. require_once (PATH_CORE.'/classes/content.class.php');
  287. $cObj=new content($this->db);
  288. $postStory=array();
  289. $x=0;
  290. while ($data=$this->db->readQ($q)) {
  291. $this->db->log($data->id);
  292. // fetch contentid from siteContentid
  293. $si=$cObj->getById($data->itemid);
  294. if ($si!==false) {
  295. $postStory[$x][logid]=$data->id;
  296. $postStory[$x][siteContentId]=$data->itemid;
  297. $postStory[$x][uid]=$data->ncUid;
  298. $postStory[$x][title]=$si->title;
  299. $postStory[$x][url]=$si->url;
  300. $postStory[$x][date]=$si->date;
  301. $postStory[$x][caption]=htmlentities($utilObj->shorten(strip_tags($si->caption),500),ENT_QUOTES);
  302. $imageProps=$cObj->getImage($data->itemid);
  303. if ($imageProps!==false)
  304. $postStory[$x][imageurl]=$imageProps->url;
  305. $x+=1;
  306. }
  307. }
  308. $log['postStory']=$postStory;
  309. }
  310. $this->narrate('Posting stories to NewsCloud');
  311. $this->narrate($postStory);
  312. /*** Transmit comments - req. ncUid and contentid ***/
  313. $comments=array();
  314. $q=$this->db->query("SELECT Log.id,Comments.comments,Comments.siteCommentId,Content.contentid,User.ncUid FROM Log,Comments,Content,User WHERE Log.itemid=Comments.siteCommentId AND Comments.siteContentId=Log.itemid2 AND Content.siteContentId=Log.itemid2 AND Content.contentid>0 AND Log.userid1=User.userid AND User.ncUid>0 AND action='comment' AND status='pending' AND Content.isBlocked=0 AND User.isBlocked=0 AND Comments.videoid=0 ORDER BY id DESC LIMIT $limit;");
  315. $x=0;
  316. while ($data=$this->db->readQ($q)) {
  317. $comments[$x][uid]=$data->ncUid;
  318. $comments[$x][contentid]=$data->contentid;
  319. $comments[$x][logid]=$data->id;
  320. $comments[$x][comments]=htmlentities($data->comments,ENT_QUOTES);
  321. $comments[$x][siteCommentId]=$data->siteCommentId;
  322. $x+=1;
  323. // fetch contentid from siteContentid
  324. //$contentid=$this->lookupContentId($data->itemid);
  325. //if ($contentid!==false AND $contentid<>0) {
  326. // fetch newscloud id from userid1
  327. //$ncUid=$this->lookupUid($data->userid1);
  328. //if ($ncUid<>0) {
  329. //}
  330. //}
  331. }
  332. $log['comments']=$comments;
  333. $this->narrate('Transmitting comments');
  334. $this->narrate($comments);
  335. // send serialized array out
  336. return serialize($log);
  337. }
  338. function lookupUid($userid=0) {
  339. $q=$this->db->query("SELECT ncUid FROM User WHERE userid=$userid;");
  340. $data=$this->db->readQ($q);
  341. return $data->ncUid;
  342. }
  343. function lookupContentId($siteContentId=0) {
  344. $q=$this->db->queryC("SELECT contentid FROM Content WHERE siteContentId=$siteContentId;");
  345. if ($q===false)
  346. return false;
  347. $data=$this->db->readQ($q);
  348. return $data->contentid;
  349. }
  350. function receive($rx='') {
  351. $msg='';
  352. // update database based on log synchronization results
  353. $resp=unserialize($rx);
  354. // process log result and perform appropriate actions
  355. // process vote results
  356. $votes=$resp['votes'];
  357. if (count($votes)>0 AND is_array($votes)) {
  358. foreach ($votes as $item) {
  359. $this->processResultStatus($item[logid],$item[result]);
  360. $msg.=$item[logid].'->'.$item[result].'<br />';
  361. }
  362. }
  363. // process comment results
  364. $comments=$resp['comments'];
  365. if (count($comments)>0 AND is_array($comments)) {
  366. foreach ($comments as $item) {
  367. $this->processResultStatus($item[logid],$item[result]);
  368. // sync NC commentid
  369. $commentid=$item[commentid];
  370. $siteCommentId=$item[siteCommentId];
  371. $this->db->update("Comments","commentid=$commentid","siteCommentId=$siteCommentId");
  372. $msg.=$item[logid].'->'.$item[result].' and commentid: '.$commentid.'<br />';
  373. }
  374. }
  375. // process readStory results
  376. $readStory=$resp['readStory'];
  377. if (count($readStory)>0 AND is_array($readStory)) {
  378. foreach ($readStory as $item) {
  379. $this->processResultStatus($item[logid],$item[result]);
  380. $msg.=$item[logid].'->'.$item[result].'<br />';
  381. }
  382. }
  383. // process pubStory results
  384. $pubStory=$resp['pubStory'];
  385. if (count($pubStory)>0 AND is_array($pubStory)) {
  386. foreach ($pubStory as $item) {
  387. $this->processResultStatus($item[logid],$item[result]);
  388. $msg.=$item[logid].'->'.$item[result].'<br />';
  389. }
  390. }
  391. // process postStory results
  392. $postStory=$resp['postStory'];
  393. if (count($postStory)>0 AND is_array($postStory)) {
  394. foreach ($postStory as $item) {
  395. $contentid=$item[contentid];
  396. $imageid=$item[imageid];
  397. $siteContentId=$item[siteContentId];
  398. $this->processResultStatus($item[logid],$item[result]);
  399. // synchronize contentid
  400. $this->db->update("Content","contentid=$contentid,imageid=$imageid","siteContentId=$siteContentId");
  401. $this->db->update("Comments","contentid=$contentid","siteContentId=$siteContentId");
  402. $msg.='Post story:'.$item[logid].'->'.$item[result].'<br />';
  403. }
  404. }
  405. // process pubWire results
  406. $pubWire=$resp['pubWire'];
  407. if (count($pubWire)>0 AND is_array($pubWire)) {
  408. foreach ($pubWire as $item) {
  409. $siteContentId=$item[siteContentId];
  410. $contentid=$item[contentid];
  411. $this->processResultStatus($item[logid],$item[result]);
  412. // synchronize contentid
  413. $this->db->update("Content","contentid=$contentid","siteContentId=$siteContentId");
  414. $this->db->update("Comments","contentid=$contentid","siteContentId=$siteContentId");
  415. $msg.=$item[logid].'->'.$item[result].' and '.$contentid.' <= '.$siteContentId.'<br />';
  416. }
  417. }
  418. // process registered users
  419. $newUsers=$resp['newUsers'];
  420. if (count($newUsers)>0 AND is_array($newUsers)) {
  421. foreach ($newUsers as $item) {
  422. $userid=$item[userid];
  423. $ncUid=$item[ncUid];
  424. $name=$item[name];
  425. $this->db->update("User","ncUid=$ncUid","userid=$userid");
  426. $this->db->update("Content","postedById=$ncUid","userid=$userid");
  427. $this->db->update("Comments","postedById=$ncUid","userid=$userid");
  428. $msg.=$item[userid].'->'.$item[result].' and userid '.$userid.' <=> ncUid'.$ncUid.'<br />';
  429. }
  430. }
  431. // process level changes
  432. $msg.='<h3>User Level Changes</h3>';
  433. $levelChanges=$resp['levelChanges'];
  434. if (count($levelChanges)>0 AND is_array($levelChanges)) {
  435. foreach ($levelChanges as $item) {
  436. $this->processResultStatus($item[logid],$item[result]);
  437. $msg.=$item[logid].'->'.$item[result].'<br />';
  438. }
  439. }
  440. return $msg;
  441. }
  442. function processResultStatus($logid=0,$result='') {
  443. switch ($result) {
  444. case 'ok':
  445. $this->db->update("Log","status='ok'","id=$logid");
  446. break;
  447. }
  448. }
  449. function serialize($id=0,$userid1=0,$action='',$itemid=0,$userid2=0,$itemid2=0) {
  450. // creates an object for an action
  451. $data= new stdClass;
  452. $data->id=$id;
  453. $data->userid1=$userid1;
  454. $data->action=$action;
  455. $data->itemid=$itemid;
  456. $data->userid2=$userid2;
  457. $data->itemid2=$itemid2;
  458. return $data;
  459. }
  460. function add($log) {
  461. $log->id = $this->db->insert("Log","itemid,userid1,userid2,action,itemid2,dateCreated","$log->itemid,$log->userid1,$log->userid2,'$log->action',$log->itemid2,NOW()");
  462. $this->checkSubmitSiteChallenge($log);
  463. }
  464. function update($log) {
  465. if ($debug) $this->db->log( 'loginfo: <pre>'.print_r($log, true). '</pre>');
  466. // update Log row in db from data object
  467. $this->db->query("SELECT * FROM Log WHERE userid1=$log->userid1 AND action='$log->action' AND itemid=$log->itemid AND userid2=$log->userid2;");
  468. if ($this->db->count()==0) {
  469. // add new log
  470. //$log->id= $this->db->insert("Log","itemid,itemid2,userid1,userid2,action,dateCreated","$log->itemid,$log->itemid2,$log->userid1,$log->userid2,'$log->action',NOW()");
  471. // exhibit A why this approach is insane:
  472. $log->id = $this->db->insert("Log","itemid,userid1,userid2,action,itemid2,dateCreated","$log->itemid,$log->userid1,$log->userid2,'$log->action',$log->itemid2,NOW()");
  473. $this->checkSubmitSiteChallenge($log);
  474. return true;
  475. } else {
  476. // update log
  477. $this->db->update("Log","userid1=$log->userid1,userid2=$log->userid2,action='$log->action',itemid=$log->itemid","id=$log->id");
  478. return false;
  479. }
  480. }
  481. function setStatus($id,$status='ok') {
  482. $this->db->update("Log","status='$status'","id=$id");
  483. }
  484. function setFeedPublishStatus($id,$status='complete') {
  485. $this->db->update("Log","isFeedPublished='$status'","id=$id");
  486. }
  487. function updateFromPublisher($log){
  488. $this->db->insert("Log","itemid,userid1,userid2,action,isFeedPublished","$log->itemid,$log->userid1,$log->userid2,'$log->action','complete'");
  489. }
  490. /*
  491. * Master list of hooked site actions that can trigger challenges
  492. second column specifies logging behavior - auto challenges that we want to show as completed challenges in the feed should set to false
  493. true => dont log separately as completed challenges - useful if the action feed will show a special story already or if it isnt important to be seen on the action feed at all
  494. false => also log as completing a challenge
  495. */
  496. static $siteChallengeActions = array(
  497. 'vote' => true, // in - but should it be?
  498. 'comment' => true, // in
  499. 'invite' => true, // in
  500. 'postStory' => true, // in
  501. 'postBlog' => true, // in
  502. 'publishWire' => true, // ?
  503. 'publishStory' => true, // ?
  504. 'shareStory' => true, // in
  505. 'referReader' => true, // ?
  506. 'postTwitter' => true, // ?
  507. 'signup' => true, // tested
  508. 'addedWidget' => false, // which widget?
  509. 'addedFeedHeadlines' => false, // NOT in
  510. 'addBookmarkTool'=> true, // tested
  511. 'friendSignup' => true, // tested (i think)
  512. 'levelIncrease' => true, // tested
  513. 'chatStory' => true, // new
  514. );
  515. function checkSubmitSiteChallenge($log)
  516. {
  517. //echo 'log action:' . $log->action . '<br>';
  518. if (!(array_search($log->action, array_keys(self::$siteChallengeActions))===false)) // lame
  519. {
  520. //echo 'found action in siteChallengeActions<br>';
  521. require_once(PATH_CORE .'/classes/challenges.class.php');
  522. $ct = new ChallengeCompletedTable($this->db);
  523. if (!$ct->submitAutomaticChallenge(
  524. $log->userid1, $log->action, &$statuscode,self::$siteChallengeActions[$log->action], $log->id)) // returns false if it couldnt be approved
  525. {
  526. //echo $statuscode; // TODO: take this out when done testing
  527. //$this->db->log($statuscode);
  528. }
  529. //echo $statuscode; // TODO: take this out when done testing
  530. //$this->db->log("checkSubmitSiteChallenge $log->action: $statuscode dontLog: ".self::$siteChallengeActions[$log->action]);
  531. //$this->db->log(print_r(self::$siteChallengeActions, true));
  532. // update cached user vars for select site actions
  533. require_once(PATH_CORE.'/classes/user.class.php');
  534. $ut = new UserTable($this->db);
  535. $uit = new UserInfoTable($this->db);
  536. $user = $ut->getRowObject();
  537. $userinfo = $uit->getRowObject();
  538. switch ($log->action)
  539. {
  540. case 'invite':
  541. if ($userinfo->load($log->userid1)) { $userinfo->cachedFriendsInvited++; $userinfo->update(); } break;
  542. case 'comment':
  543. if ($user->load($log->userid1)) { $user->cachedCommentsPosted++; $user->update(); } break;
  544. case 'postStory':
  545. case 'postBlog':
  546. if ($user->load($log->userid1)) { $user->cachedStoriesPosted++; $user->update(); } break;
  547. default: break;
  548. }
  549. }
  550. }
  551. function checkLimits($userid=0,$whereStr='',$actionStr='',$nickel=5,$hour=7,$day=25) {
  552. $error=false;
  553. $errorMsg='';
  554. // Make sure user has not exceeded their rate limits
  555. $actionLimits = array(
  556. 'nickel' => $nickel,
  557. 'hour' => $hour,
  558. 'day' => $day
  559. );
  560. $limitSql = "SELECT COUNT(CASE WHEN t > '".date("Y-m-d H:i:s", time() - (5 * 60))."' THEN 1 ELSE null END) AS nickel, COUNT(CASE WHEN t > '".date("Y-m-d H:i:s", time() - (60 * 60))."' THEN 1 ELSE null END) as hour, COUNT(CASE WHEN t > '".date("Y-m-d 00:00:00", time())."' THEN 1 ELSE null END) as day FROM Log WHERE userid1 = $userid AND $whereStr";
  561. // to do - remove after debug
  562. $results = $this->db->query($limitSql);
  563. $actionTotals = mysql_fetch_assoc($results);
  564. if ($actionTotals['day'] >= $actionLimits['day']) {
  565. $error = true;
  566. $errorMsg = 'You have exceeded your rate limit for '.$actionStr.'. Please try again in one day.';
  567. } else if ($actionTotals['hour'] >= $actionLimits['hour']) {
  568. $error = true;
  569. $errorMsg = 'You have exceeded your rate limit for '.$actionStr.'. Please try again in one hour.';
  570. } else if ($actionTotals['nickel'] >= $actionLimits['nickel']) {
  571. $error = true;
  572. $errorMsg = 'You have exceeded your rate limit for '.$actionStr.'. Please try again in 5 mins.';
  573. }
  574. if ($error) {
  575. $result=array();
  576. $result['error']=$error;
  577. $result['msg']=$errorMsg;
  578. return $result;
  579. } else
  580. return false;
  581. }
  582. }
  583. ?>