PageRenderTime 62ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/wordbooker/includes/wordbooker_cron.php

https://bitbucket.org/crypticrod/sr_wp_code
PHP | 362 lines | 269 code | 24 blank | 69 comment | 48 complexity | 0045715b36f1009da555f7eafe7f18dc MD5 | raw file
Possible License(s): AGPL-1.0, GPL-2.0, LGPL-2.1, GPL-3.0, LGPL-2.0, AGPL-3.0
  1. <?php
  2. /**
  3. Extension Name: Wordbooker Cron
  4. Extension URI: http://blogs.canalplan.org.uk/steve
  5. Version: 2.0.8
  6. Description: Collection of processes that are often handled by wp_cron scheduled jobs
  7. Author: Steve Atty
  8. */
  9. function wordbooker_cache_refresh ($user_id) {
  10. global $blog_id,$wpdb,$table_prefix,$wordbooker_user_settings_id,$wbooker_user_id;
  11. $wbooker_user_id=$user_id;
  12. $result = $wpdb->query(' DELETE FROM ' . WORDBOOKER_ERRORLOGS . ' WHERE blog_id ='.$blog_id.' and (user_ID='.$user_id.' or user_ID=0 ) and post_id<=0');
  13. wordbooker_debugger("Cache Refresh Commence ",$user_id,0) ;
  14. $result = $wpdb->get_row("select facebook_id from ".WORDBOOKER_USERDATA." where user_ID=".$user_id);
  15. $uid=$result->facebook_id;
  16. $wbuser2= wordbooker_get_userdata($user_id);
  17. $wordbooker_settings =get_option('wordbooker_settings');
  18. wordbooker_debugger("Cache Refresh for ",$wbuser2->name,0) ;
  19. wordbooker_debugger("UID length : ",strlen($uid),0) ;
  20. # If we've not got the ID from the table lets try to get it from the logged in user
  21. if (strlen($uid)==0) {
  22. wordbooker_debugger("No Cache record for user - getting Logged in user ",$uid,0) ;
  23. try {
  24. $x=wordbooker_get_fb_id($wbuser2->access_token);
  25. $uid=$x->id;
  26. }
  27. catch (Exception $e) {
  28. $error_code = $e->getCode();
  29. $error_msg = $e->getMessage();
  30. wordbooker_debugger($error_msg," ",0) ;
  31. #unset($uid);
  32. }
  33. }
  34. # If we now have a uid lets go and do a few things.
  35. if (strlen($uid)>0){
  36. wordbooker_debugger("Cache processing for user : ",$wbuser2->name." (".$uid.")",0) ;
  37. wordbooker_debugger("Getting Permisions for : ",$uid,0) ;
  38. $ret=wordbooker_fb_pemissions($wbuser2->facebook_id,$wbuser2->access_token);
  39. # If we have an $ret->error->message then we have a problem
  40. # wordbooker_debugger("Serialized object ",serialize($ret),0) ;
  41. # wordbooker_debugger("Serialized object length ",strlen(serialize($ret)),0) ;
  42. #var_dump($ret->error);
  43. # wordbooker_debugger("Object Error ",$ret->error->message,0) ;
  44. if($ret->error->message) {
  45. wordbooker_append_to_errorlogs("Your Facebook Session is invalid", "99", $ret->error->message,'',$user_id);
  46. wordbooker_delete_user($user_id,1);
  47. return;
  48. }
  49. if(strlen(serialize($ret))<20) {wordbooker_debugger("Permissions fetch failed - skipping ",'',0) ;} else {
  50. $add_auths=0;
  51. $permlist= array(WORDBOOKER_FB_PUBLISH_STREAM,WORDBOOKER_FB_STATUS_UPDATE,WORDBOOKER_FB_READ_STREAM,WORDBOOKER_FB_CREATE_NOTE,WORDBOOKER_FB_PHOTO_UPLOAD,WORDBOOKER_FB_VIDEO_UPLOAD,WORDBOOKER_FB_MANAGE_PAGES,WORDBOOKER_FB_READ_FRIENDS);
  52. $key=0;
  53. foreach($permlist as $perm){
  54. try {
  55. $permy=$ret->data[0]->$perm;
  56. $error_code = null;
  57. if($permy!=1) {
  58. wordbooker_debugger("User is missing permssion : ",$perm,0) ;
  59. $add_auths = $add_auths | pow(2,$key);
  60. }
  61. else {
  62. wordbooker_debugger("User has permssion : ",$perm,0) ;
  63. }
  64. $error_msg = null;
  65. } catch (Exception $e) {
  66. $error_msg = $e->getMessage();
  67. wordbooker_debugger("Permissions may be corrupted ",$error_message,0);
  68. $users = null;
  69. $add_auths=1;
  70. }
  71. $key=$key+1;
  72. }
  73. wordbooker_debugger("Additional Permissions needed : ",$add_auths,0) ;
  74. $sql="update ".WORDBOOKER_USERDATA." set auths_needed=".$add_auths." where user_ID=".$user_id;
  75. $result = $wpdb->get_results($sql);
  76. }
  77. # Lets get the person/page this user wants to get the status for. We get this from the user_meta
  78. $wordbooker_user_settings_id="wordbookuser".$blog_id;
  79. $wordbookuser_setting=get_usermeta($user_id,$wordbooker_user_settings_id);
  80. $suid="PW:".$uid;
  81. if ( isset ($wordbookuser_setting['wordbooker_status_id']) && $wordbookuser_setting['wordbooker_status_id']!=-100) {$suid=$wordbookuser_setting['wordbooker_status_id'];}
  82. $x=explode(":",$suid);
  83. $suid=$x[1];
  84. wordbooker_debugger("Getting Status for : ",$suid,0) ;
  85. try {
  86. $query="SELECT uid,time,message FROM status WHERE uid= $suid limit 1";
  87. $fb_status_info=wordbooker_fql_query($query,$wbuser2->access_token);
  88. }
  89. catch (Exception $e) {
  90. $error_msg = $e->getMessage();
  91. wordbooker_debugger("Failed to get Status : ",$error_msg,0);;
  92. }
  93. try {
  94. $query="SELECT name, url, pic FROM profile WHERE id=$suid ";
  95. $fb_profile_info = wordbooker_fql_query($query,$wbuser2->access_token);
  96. if(is_array($fb_profile_info)){$fb_profile_info= $fb_profile_info[0];}
  97. }
  98. catch (Exception $e) {
  99. $error_msg = $e->getMessage();
  100. wordbooker_debugger("Failed to get user info : ",$error_msg,0);
  101. }
  102. wordbooker_debugger("Getting Pages administered by : ",$uid,0) ;
  103. try {
  104. $query="SELECT name, page_url, page_id FROM page WHERE page_id IN (SELECT page_id FROM page_admin WHERE uid= $uid )";
  105. $fb_page_info = wordbooker_fql_query($query,$wbuser2->access_token);
  106. }
  107. catch (Exception $e)
  108. {
  109. $error_msg = $e->getMessage();
  110. wordbooker_debugger("Failed to get page info : ",$error_msg,0);
  111. }
  112. try {
  113. $ret_code=wordbooker_me($wbuser2->access_token);
  114. }
  115. catch (Exception $e)
  116. {
  117. $error_msg = $e->getMessage();
  118. wordbooker_debugger("Failed to get page tokens : ".$error_msg," ",0);
  119. }
  120. if (isset($ret_code->data)){
  121. foreach($ret_code->data as $page_access) {
  122. $page_token[$page_access->id]=$page_access->access_token;
  123. }
  124. $all_pages=array();
  125. if (is_array($fb_page_info)) {
  126. $encoded_names=str_replace('\\','\\\\',serialize($fb_page_info));
  127. foreach ( $fb_page_info as $pageinfo ) {
  128. $pages["id"]="FW:".trim($pageinfo->page_id,',');
  129. if(strlen($pageinfo->name)>1){
  130. $page_desc="";
  131. if(strpos($pageinfo->page_url,"application.php")) {$page_desc=__(" (Application)", 'wordbooker');}
  132. if (function_exists('mb_convert_encoding')) {
  133. $pages["name"]=mb_convert_encoding($pageinfo->name.$page_desc,'UTF-8');
  134. }
  135. else
  136. {
  137. $pages["name"]=$pageinfo->name.$page_desc;
  138. }
  139. $pages['url']=$pageinfo->page_url;
  140. $pages["access_token"]=$page_token[$pageinfo->page_id];
  141. $all_pages[]=$pages;
  142. wordbooker_debugger("Page info for page ID ".$pageinfo->page_id,$pageinfo->name.$page_desc,0) ;
  143. }
  144. }
  145. }
  146. else {
  147. wordbooker_debugger("Failed to get page information from FB"," ",0);
  148. }
  149. }
  150. $fb_group_list=array();
  151. $all_groups=array();
  152. wordbooker_debugger("Getting Groups owned or managed by : ",$uid,0) ;
  153. try {
  154. $query="Select positions, gid from group_member where uid=$uid";
  155. $fb_groups= wordbooker_fql_query($query,$wbuser2->access_token);
  156. if(is_array($fb_groups)){
  157. foreach($fb_groups as $fb_group){
  158. # Check to see if there are any positions. If not then the user is only a member of the group and thus we dont want it in the list.
  159. if(count($fb_group->positions)>0) {
  160. wordbooker_debugger("Getting details for group : ",$fb_group->gid,0) ;
  161. $query="Select name,gid from group where gid =".$fb_group->gid;
  162. $fb_group_info= wordbooker_fql_query($query,$wbuser2->access_token);
  163. $fb_group_list[]= (array) $fb_group_info;
  164. }
  165. }
  166. }
  167. }
  168. catch (Exception $e)
  169. {
  170. $error_msg = $e->getMessage();
  171. wordbooker_debugger("Failed to get group info : ",$error_msg,0);
  172. }
  173. if (is_array($fb_group_list)) {
  174. $encoded_names=str_replace('\\','\\\\',serialize($fb_group_list));
  175. foreach ( $fb_group_list as $groupinfo ) {
  176. $groupinfo = (array) $groupinfo;
  177. if (strlen($groupinfo[0]->gid) >1) {
  178. $groups["page_id"]=trim($groupinfo[0]->gid,',');
  179. $groups["id"]="GW:".trim($groupinfo[0]->gid,',');
  180. if (function_exists('mb_convert_encoding')) {
  181. $groups["name"]=mb_convert_encoding($groupinfo[0]->name,'UTF-8');
  182. }
  183. else
  184. {
  185. $groups["name"]=$groupinfo[0]->name;
  186. }
  187. $groups["access_token"]="dummy access token";
  188. $all_groups[]=$groups;
  189. wordbooker_debugger("Group info for group ID ".$groupinfo[0]->gid,$groupinfo[0]->name,0) ;
  190. }
  191. }
  192. }
  193. else {
  194. wordbooker_debugger("Failed to get group information from FB"," ",0);
  195. }
  196. try {
  197. $query="Select is_app_user FROM user where uid=$uid";
  198. $fb_app_info = wordbooker_fql_query($query,$wbuser2->access_token);
  199. }
  200. catch (Exception $e)
  201. {
  202. $error_msg = $e->getMessage();
  203. wordbooker_debugger("Failed to get app_user inf : ",$error_msg,0);
  204. }
  205. $all_pages_groups=@array_merge($all_pages,$all_groups);
  206. $encoded_names=str_replace('\\','\\\\',serialize($all_pages_groups));
  207. /*
  208. try {
  209. $query="SELECT flid, owner, name FROM friendlist WHERE owner=$uid";
  210. $fb_friend_lists= wordbooker_fql_query($query,$wbuser2->access_token);
  211. if (is_array($fb_friend_lists)) {
  212. $sql="Delete from ".WORDBOOKER_FB_FRIEND_LISTS." where user_id=".$user_id;
  213. $result = $wpdb->get_results($sql);
  214. foreach ($fb_friend_lists as $friend_list) {
  215. if (function_exists('mb_convert_encoding')) {
  216. $friend_list->name=mb_convert_encoding($friend_list->name,'UTF-8');
  217. }
  218. $sql="replace into ".WORDBOOKER_FB_FRIEND_LISTS." (user_id, flid, owner, name) values (".$user_id.",'".$friend_list->flid."','".$friend_list->owner."','".$friend_list->name."')";
  219. $result = $wpdb->get_results($sql);
  220. }
  221. }
  222. }
  223. catch (Exception $e)
  224. {
  225. $error_msg = $e->getMessage();
  226. wordbooker_debugger("Failed to get friend lists : ",$error_msg,0);
  227. }
  228. try {
  229. $query="Select name,uid from user where uid in (Select uid from friendlist_member where flid='10150839623220195')";
  230. $fb_friends_info = wordbooker_fql_query($query,$wbuser2->access_token);
  231. }
  232. catch (Exception $e)
  233. {
  234. $error_msg = $e->getMessage();
  235. wordbooker_debugger("Failed to get friends : ",$error_msg,0);
  236. }
  237. if (is_array($fb_friends_info) ) {
  238. $sql="delete from ".WORDBOOKER_FB_FRIENDS." where user_id=".$user_id;
  239. $result = $wpdb->get_results($sql);
  240. foreach ($fb_friends_info as $friend_info) {
  241. if (function_exists('mb_convert_encoding')) {
  242. $friend_info->name=mb_convert_encoding($friend_info->name,'UTF-8');
  243. }
  244. $sql="insert into ".WORDBOOKER_FB_FRIENDS." (user_id, facebook_id, name, blog_id) values (".$user_id.",'".$friend_info->uid."','".$friend_info->name."',".$blog_id.")";
  245. $result = $wpdb->get_results($sql);
  246. }
  247. }
  248. */
  249. wordbooker_debugger("Setting Status Name as : ",mysql_real_escape_string($fb_profile_info->name),0) ;
  250. $sql="insert into ".WORDBOOKER_USERSTATUS." set name='".mysql_real_escape_string($fb_profile_info->name)."'";
  251. if(is_array($fb_status_info)) {$fb_status_info=$fb_status_info[0];}
  252. if (isset($fb_status_info->time)) {
  253. if (stristr($fb_status_info->message,"[[PV]]")) {
  254. wordbooker_debugger("Found [[PV]] - not updating status"," ",0);
  255. }
  256. else {
  257. wordbooker_debugger("Setting status as : ",mysql_real_escape_string($fb_status_info->message),0) ;
  258. $sql.=", status='".mysql_real_escape_string($fb_status_info->message)."'";
  259. $sql.=", updated=".mysql_real_escape_string($fb_status_info->time);
  260. }
  261. } else {wordbooker_debugger("Failed to get Status information from FB"," ",0); }
  262. if (isset($fb_profile_info->url)) {
  263. wordbooker_debugger("Setting Status URL as : ",mysql_real_escape_string($fb_profile_info->url),0) ;
  264. $sql.=", url='".mysql_real_escape_string($fb_profile_info->url)."'";
  265. $sql.=", pic='".mysql_real_escape_string($fb_profile_info->pic)."'";
  266. } else {wordbooker_debugger("Failed to get Image information from FB"," ",0); }
  267. $sql.=", facebook_id='".$uid."'";
  268. $sql.=",user_ID=".$user_id;
  269. $sql.=",blog_id=".$blog_id;
  270. $sql.=" on duplicate key update name='".mysql_real_escape_string($fb_profile_info->name)."'";
  271. if (isset($fb_status_info->message)) {
  272. if (stristr($fb_status_info->message,"[[PV]]")) {
  273. }
  274. else {
  275. $sql.=", status='".mysql_real_escape_string($fb_status_info->message)."'";
  276. $sql.=", updated=".mysql_real_escape_string($fb_status_info->time);
  277. }
  278. }
  279. if (isset($fb_profile_info->url)) {
  280. $sql.=", url='".mysql_real_escape_string($fb_profile_info->url)."'";
  281. $sql.=", pic='".mysql_real_escape_string($fb_profile_info->pic)."'";
  282. }
  283. $result = $wpdb->get_results($sql);
  284. $real_user=wordbooker_get_fb_id($wbuser2->access_token);
  285. wordbooker_debugger("Setting user name as : ",mysql_real_escape_string($real_user->name),0) ;
  286. $sql="update ".WORDBOOKER_USERDATA." set name='".mysql_real_escape_string($real_user->name)."'";
  287. $sql.=", facebook_id='".$uid."'";
  288. $sql.=", pages= '".mysql_real_escape_string($encoded_names)."'";
  289. if (is_array($fb_app_info)) {
  290. $sql.=", use_facebook=".(integer) $fb_app_info[0]->is_app_user;
  291. }
  292. $sql.=" where user_ID=".$user_id." and blog_id=".$blog_id;
  293. $result = $wpdb->get_results($sql);
  294. }
  295. #fclose($fp);
  296. wordbooker_debugger("Cache Refresh Complete for user",$uid,0) ;
  297. }
  298. function wordbooker_poll_facebook($single_user=null) {
  299. global $wpdb, $user_id,$table_prefix,$blog_id;
  300. # If a user ID has been passed in then restrict to that single user.
  301. wordbooker_trim_errorlogs();
  302. $limit_user="";
  303. if (isset($single_user)) {$limit_user=" where user_id=".$single_user." limit 1";}
  304. $wordbooker_settings =get_option('wordbooker_settings');
  305. # This runs through the Cached users and refreshes them
  306. $sql="Select user_id,name from ".WORDBOOKER_USERDATA.$limit_user;
  307. $wb_users = $wpdb->get_results($sql);
  308. if (is_array($wb_users)) {
  309. wordbooker_debugger("Batch Cache Refresh Commence "," ",0) ;
  310. foreach ($wb_users as $wb_user){
  311. wordbooker_debugger("Calling Cache refresh for : ",$wb_user->name." (".$wb_user->id.")",0) ;
  312. $wbuser = wordbooker_get_userdata($wb_user->user_id);
  313. # $fbclient = wordbooker_fbclient($wbuser);
  314. wordbooker_cache_refresh($wb_user->user_id);
  315. }
  316. wordbooker_debugger("Batch Cache Refresh completed "," ",0) ;
  317. }
  318. if ( !isset($wordbooker_settings['wordbooker_comment_get'])) {
  319. wordbooker_debugger("Comment Scrape not active. Cron Finished "," ",0) ;
  320. return;
  321. }
  322. // Yes they have so lets get to work. We have to get the FB users associated with this blog
  323. $sql="Select user_id from ".WORDBOOKER_USERDATA." where blog_id=".$blog_id." ".$limit_user;
  324. $wb_users = $wpdb->get_results($sql);
  325. if (!is_array($wb_users)) {
  326. return;
  327. }
  328. return;
  329. # Comment handling has been de-activated for the initial release of V2
  330. foreach ($wb_users as $wb_user){
  331. wordbooker_pull_comments($wb_user);
  332. } // end of foreach user
  333. }
  334. ?>