Cheetah
ChSpyDb.php
Go to the documentation of this file.
1 <?php
2 
8  require_once( CH_DIRECTORY_PATH_CLASSES . 'ChWsbModuleDb.php' );
9 
10  class ChSpyDb extends ChWsbModuleDb
11  {
12  var $_oConfig;
14 
18  function __construct(&$oConfig)
19  {
20  parent::__construct();
21 
22  $this -> _oConfig = $oConfig;
23  $this -> sTablePrefix = $oConfig -> getDbPrefix();
24  }
25 
32  {
33  $sQuery = "SELECT * FROM `{$this->sTablePrefix}handlers`";
34  return $this -> getAll($sQuery);
35  }
36 
43  function getActivityCount($sType = '')
44  {
46 
47  $sWhere = '';
48  if($sType && $sType != 'all'){
49  $sWhere = "WHERE `type` = '{$sType}'";
50  }
51 
52  $sQuery = "SELECT COUNT(*) FROM `{$this->sTablePrefix}data` {$sWhere}";
53  !($iCount = $this -> getOne($sQuery) ) ? $iCount = 0 : null;
54 
55  return $iCount;
56  }
57 
64  function getLastActivityId($sType = '')
65  {
67  $sWhere = '';
68 
69  if($sType && $sType != 'all'){
70  $sWhere = "WHERE `type` = '{$sType}'";
71  }
72 
73  $sQuery = "SELECT `id` FROM `{$this->sTablePrefix}data` {$sWhere} ORDER BY `id` DESC LIMIT 1";
74  !($iLastEventId = $this -> getOne($sQuery) ) ? $iLastEventId = 0 : null;
75 
76  return $iLastEventId;
77  }
78 
87  {
88  $iProfileId = (int) $iProfileId;
90  $sWhere = '';
91 
92  if($sType && $sType != 'all'){
93  $sWhere = " AND `ch_spy_data`.`type` = '{$sType}'";
94  }
95 
96  $sQuery =
97  "
98  SELECT
99  `ch_spy_data`.`id`
100  FROM
101  `ch_spy_data`
102  INNER JOIN
103  `ch_spy_friends_data`
104  ON
105  `ch_spy_friends_data`.`event_id` = `ch_spy_data`.`id`
106  WHERE
107  `ch_spy_friends_data`.`friend_id` = {$iProfileId}
108  AND
109  `ch_spy_data`.`sender_id` <> {$iProfileId}
110  {$sWhere}
111  ORDER BY
112  `ch_spy_data`.`id` DESC LIMIT 1
113  ";
114 
115  !($iLastEventId = $this -> getOne($sQuery) ) ? $iLastEventId = 0 : null;
116  return $iLastEventId;
117  }
118 
127  {
128  $iProfileId = (int) $iProfileId;
130  $sWhere = '';
131 
132  if($sType && $sType != 'all'){
133  $sWhere = " AND `ch_spy_data`.`type` = '{$sType}'";
134  }
135 
136  $sQuery =
137  "
138  SELECT
139  COUNT(`ch_spy_data`.`id`)
140  FROM
141  `ch_spy_data`
142  INNER JOIN
143  `ch_spy_friends_data`
144  ON
145  `ch_spy_friends_data`.`event_id` = `ch_spy_data`.`id`
146  WHERE
147  `ch_spy_friends_data`.`friend_id` = {$iProfileId}
148  AND
149  `ch_spy_data`.`sender_id` <> {$iProfileId}
150  {$sWhere}
151  ";
152 
153  !($iCount = $this -> getOne($sQuery) ) ? $iCount = 0 : null;
154  return $iCount;
155  }
156 
162  function getSettingsCategory($sValueName)
163  {
164  $sValueName = process_db_input($sValueName, CH_TAGS_STRIP);
165  return $this -> getOne('SELECT `kateg` FROM `sys_options` WHERE `Name` = "' . $sValueName . '"');
166  }
167 
174  function setViewed($iActivityId)
175  {
176  $iActivityId = (int) $iActivityId;
177  $sQuery = "UPDATE `{$this->sTablePrefix}data` SET `viewed` = 1";
178  $this -> query($sQuery);
179  }
180 
188  {
189  $iProfileId = (int) $iProfileId;
190  $sQuery = "UPDATE `{$this->sTablePrefix}data` SET `viewed` = 1 WHERE `recipient_id` = {$iProfileId}";
191  $this -> query($sQuery);
192  }
193 
194  function insertData(&$aData)
195  {
196  //--- Update Spy Handlers ---//
197  foreach($aData['handlers'] as $aHandler) {
198  $aHandler['alert_unit'] = process_db_input($aHandler['alert_unit'], CH_TAGS_STRIP, CH_SLASHES_NO_ACTION);
199  $aHandler['alert_action'] = process_db_input($aHandler['alert_action'], CH_TAGS_STRIP, CH_SLASHES_NO_ACTION);
200  $aHandler['module_uri'] = process_db_input($aHandler['module_uri'], CH_TAGS_STRIP, CH_SLASHES_NO_ACTION);
201  $aHandler['module_class'] = process_db_input($aHandler['module_class'], CH_TAGS_STRIP, CH_SLASHES_NO_ACTION);
202  $aHandler['module_method'] = process_db_input($aHandler['module_method'], CH_TAGS_STRIP, CH_SLASHES_NO_ACTION);
203 
204  $sQuery =
205  "
206  INSERT INTO
207  `{$this->sTablePrefix}handlers`
208  SET
209  `alert_unit` = '{$aHandler['alert_unit']}',
210  `alert_action` = '{$aHandler['alert_action']}',
211  `module_uri` = '{$aHandler['module_uri']}',
212  `module_class` = '{$aHandler['module_class']}',
213  `module_method` = '{$aHandler['module_method']}'
214  ";
215 
216  $this -> query($sQuery);
217  }
218 
219  $sAlertName = $this -> _oConfig -> getAlertSystemName();
220 
221  //--- Update System Alerts ---//
222  $sQuery =
223  "
224  SELECT
225  `id`
226  FROM
227  `sys_alerts_handlers`
228  WHERE
229  `name`= ?
230  LIMIT 1
231  ";
232 
233  $iHandlerId = (int) $this -> getOne($sQuery, [$sAlertName]);
234 
235  foreach($aData['alerts'] as $aAlert) {
236  $aAlert['unit'] = process_db_input($aAlert['unit'], CH_TAGS_STRIP, CH_SLASHES_NO_ACTION);
237  $aAlert['action'] = process_db_input($aAlert['action'], CH_TAGS_STRIP, CH_SLASHES_NO_ACTION);
238 
239  $sQuery =
240  "
241  INSERT INTO
242  `sys_alerts`
243  SET
244  `unit` = '{$aAlert['unit']}',
245  `action` = '{$aAlert['action']}',
246  `handler_id` = '{$iHandlerId}'
247  ";
248 
249  $this -> query($sQuery);
250  }
251  }
252 
253  function deleteData(&$aData)
254  {
255  //--- Update Wall Handlers ---//
256  foreach($aData['handlers'] as $aHandler) {
257  $aHandler['alert_unit'] = process_db_input($aHandler['alert_unit'], CH_TAGS_STRIP, CH_SLASHES_NO_ACTION);
258  $aHandler['alert_action'] = process_db_input($aHandler['alert_action'], CH_TAGS_STRIP, CH_SLASHES_NO_ACTION);
259  $aHandler['module_uri'] = process_db_input($aHandler['module_uri'], CH_TAGS_STRIP, CH_SLASHES_NO_ACTION);
260  $aHandler['module_class'] = process_db_input($aHandler['module_class'], CH_TAGS_STRIP, CH_SLASHES_NO_ACTION);
261  $aHandler['module_method'] = process_db_input($aHandler['module_method'], CH_TAGS_STRIP, CH_SLASHES_NO_ACTION);
262 
263  $sQuery =
264  "
265  DELETE FROM
266  `{$this->sTablePrefix}handlers`
267  WHERE
268  `alert_unit` = '{$aHandler['alert_unit']}'
269  AND
270  `alert_action` = '{$aHandler['alert_action']}'
271  AND
272  `module_uri` = '{$aHandler['module_uri']}'
273  AND
274  `module_class` = '{$aHandler['module_class']}'
275  AND
276  `module_method` = '{$aHandler['module_method']}'
277  LIMIT 1
278  ";
279 
280  $this -> query($sQuery);
281  }
282 
283  // define system alert name;
284  $sAlertName = $this -> _oConfig -> getAlertSystemName();
285 
286  //--- Update System Alerts ---//
287  $sQuery =
288  "
289  SELECT
290  `id`
291  FROM
292  `sys_alerts_handlers`
293  WHERE
294  `name`= ?
295  LIMIT 1
296  ";
297 
298  $iHandlerId = (int) $this -> getOne($sQuery, [$sAlertName]);
299  foreach($aData['alerts'] as $aAlert) {
300  $aAlert['unit'] = process_db_input($aAlert['unit'], CH_TAGS_STRIP, CH_SLASHES_NO_ACTION);
301  $aAlert['action'] = process_db_input($aAlert['action'], CH_TAGS_STRIP, CH_SLASHES_NO_ACTION);
302 
303  $sQuery =
304  "
305  DELETE FROM
306  `sys_alerts`
307  WHERE
308  `unit` = '{$aAlert['unit']}'
309  AND
310  `action` = '{$aAlert['action']}'
311  AND
312  `handler_id` = '{$iHandlerId}'
313  LIMIT 1
314  ";
315 
316  $this -> query($sQuery);
317  }
318  }
319 
331  function createActivity($sAlertUnit, $sAlertAction, $iObjectId, $iCommentId, $iSenderId, $iRecipientId, $aActivityInfo)
332  {
333  $iSenderId = (int) $iSenderId;
334  $iRecipientId = (int) $iRecipientId;
335 
336  // procces recived parameters
337  $aParameters = isset($aActivityInfo['params']) ? process_db_input(serialize($aActivityInfo['params']), CH_TAGS_STRIP, CH_SLASHES_NO_ACTION) : '';
338  $sActivityType = isset($aActivityInfo['spy_type']) ? process_db_input($aActivityInfo['spy_type'],CH_TAGS_STRIP, CH_SLASHES_NO_ACTION) : 'content_activity';
339  $sLangKey = process_db_input($aActivityInfo['lang_key'], CH_TAGS_STRIP, CH_SLASHES_NO_ACTION);
340 
341  // execute query;
342  $sQuery =
343  "
344  INSERT INTO
345  `{$this->sTablePrefix}data`
346  SET
347  `alert_unit` = '{$sAlertUnit}',
348  `alert_action` = '{$sAlertAction}',
349  `object_id` = {$iObjectId},
350  `comment_id` = {$iCommentId},
351  `sender_id` = {$iSenderId},
352  `recipient_id` = {$iRecipientId},
353  `lang_key` = '{$sLangKey}',
354  `params` = '{$aParameters}',
355  `date` = TIMESTAMP( NOW() ),
356  `type` = '{$sActivityType}'
357  ";
358 
359  $this -> query($sQuery);
360  return $this -> lastId();
361  }
362 
363  function deleteActivityByObject($sUnit, $iObjectId, $iCommentId = 0)
364  {
365  $sWhereAddon = "";
366  if($iCommentId != 0)
367  $sWhereAddon = "AND `comment_id`='" . $iCommentId . "'";
368 
369  $sSql = "DELETE FROM
370  `" . $this->sTablePrefix . "data`
371  WHERE
372  `alert_unit`='" . $sUnit . "' AND
373  `object_id`='" . $iObjectId . "' " . $sWhereAddon;
374 
375  return $this->query($sSql);
376  }
377 
378  function deleteActivityByUser($iUserId)
379  {
380  $sSql = "DELETE FROM
381  `" . $this->sTablePrefix . "data`
382  WHERE
383  `sender_id`='" . $iUserId . "' OR
384  `recipient_id`='" . $iUserId . "'";
385 
386  return $this->query($sSql);
387  }
388 
397  function attachFriendEvent($iEventId, $iSenderId, $iFriendId)
398  {
399  $iEventId = (int) $iEventId;
400  $iSenderId = (int) $iSenderId;
401  $iFriendId = (int) $iFriendId;
402 
403  $sQuery =
404  "
405  INSERT INTO
406  `{$this->sTablePrefix}friends_data`
407  SET
408  `event_id` = {$iEventId},
409  `sender_id` = {$iSenderId},
410  `friend_id` = {$iFriendId}
411  ";
412 
413  $this -> query($sQuery);
414  }
415 
422  function deleteUselessData($iDays = 0)
423  {
424  $iDays = (int) $iDays;
425  if ($iDays < 1) {
426  return 0;
427  }
428 
429  $iAffectedRows = $this -> query("DELETE FROM `{$this->sTablePrefix}data` WHERE `{$this->sTablePrefix}data`.`date` < DATE_SUB(NOW(), INTERVAL $iDays DAY)");
430  $this -> query("OPTIMIZE TABLE `{$this->sTablePrefix}data`");
431 
432  $this -> query("DELETE `{$this->sTablePrefix}friends_data` FROM `{$this->sTablePrefix}friends_data` LEFT JOIN `{$this->sTablePrefix}data` ON (`{$this->sTablePrefix}data`.`id` = `{$this->sTablePrefix}friends_data`.`event_id`) WHERE `{$this->sTablePrefix}data`.`id` IS NULL");
433  $this -> query("OPTIMIZE TABLE `{$this->sTablePrefix}friends_data`");
434 
435  return $iAffectedRows;
436  }
437  }
process_db_input
process_db_input($sText, $iStripTags=0)
Definition: utils.inc.php:256
ChSpyDb\getActivityCount
getActivityCount($sType='')
Definition: ChSpyDb.php:43
ChSpyDb\__construct
__construct(&$oConfig)
Definition: ChSpyDb.php:18
ChSpyDb
Definition: ChSpyDb.php:11
ChSpyDb\deleteActivityByObject
deleteActivityByObject($sUnit, $iObjectId, $iCommentId=0)
Definition: ChSpyDb.php:363
ChSpyDb\getLastFriendsActivityId
getLastFriendsActivityId($iProfileId, $sType='')
Definition: ChSpyDb.php:86
ChSpyDb\getInternalHandlers
getInternalHandlers()
Definition: ChSpyDb.php:31
php
ChSpyDb\attachFriendEvent
attachFriendEvent($iEventId, $iSenderId, $iFriendId)
Definition: ChSpyDb.php:397
ChWsbModuleDb
Definition: ChWsbModuleDb.php:12
ChSpyDb\getFriendsActivityCount
getFriendsActivityCount($iProfileId, $sType='')
Definition: ChSpyDb.php:126
ChSpyDb\$_oConfig
$_oConfig
Definition: ChSpyDb.php:12
ChWsbDb\getAll
getAll($sQuery, $aBindings=[], $iFetchType=PDO::FETCH_ASSOC)
Definition: ChWsbDb.php:206
ChSpyDb\$sTablePrefix
$sTablePrefix
Definition: ChSpyDb.php:13
CH_SLASHES_NO_ACTION
const CH_SLASHES_NO_ACTION
Definition: utils.inc.php:30
ChSpyDb\deleteActivityByUser
deleteActivityByUser($iUserId)
Definition: ChSpyDb.php:378
ChSpyDb\setViewedProfileActivity
setViewedProfileActivity($iProfileId)
Definition: ChSpyDb.php:187
$sType
$sType
Definition: actions.inc.php:11
ChSpyDb\insertData
insertData(&$aData)
Definition: ChSpyDb.php:194
ChSpyDb\getSettingsCategory
getSettingsCategory($sValueName)
Definition: ChSpyDb.php:162
ChWsbDb\query
query($sQuery, $aBindings=[])
Definition: ChWsbDb.php:386
ChSpyDb\deleteData
deleteData(&$aData)
Definition: ChSpyDb.php:253
ChSpyDb\getLastActivityId
getLastActivityId($sType='')
Definition: ChSpyDb.php:64
ChWsbDb\getOne
getOne($sQuery, $aBindings=[], $iIndex=0)
Definition: ChWsbDb.php:263
CH_TAGS_STRIP
const CH_TAGS_STRIP
Definition: utils.inc.php:22
ChSpyDb\createActivity
createActivity($sAlertUnit, $sAlertAction, $iObjectId, $iCommentId, $iSenderId, $iRecipientId, $aActivityInfo)
Definition: ChSpyDb.php:331
$sActivityType
$sActivityType
Definition: index.php:19
ChWsbDb\lastId
lastId()
Definition: ChWsbDb.php:449
as
as
Definition: Filter.ExtractStyleBlocks.Escaping.txt:10
$iProfileId
if( $sMembersList) $iProfileId
Definition: communicator.php:29
$aParameters
$aParameters
Definition: index.php:29
ChSpyDb\deleteUselessData
deleteUselessData($iDays=0)
Definition: ChSpyDb.php:422
ChSpyDb\setViewed
setViewed($iActivityId)
Definition: ChSpyDb.php:174