PageRenderTime 66ms CodeModel.GetById 32ms RepoModel.GetById 0ms app.codeStats 1ms

/editor/functions.php

https://bitbucket.org/3tierlogic/3tl.tabbuilder
PHP | 782 lines | 694 code | 57 blank | 31 comment | 24 complexity | bd8a8ceb3f66d506dcf10f5730fd51f3 MD5 | raw file
  1. <?php
  2. session_start();
  3. //------------------- GLOBAL CONSTANTS ------------------//
  4. // uploaded files directory
  5. define("DIR_UPLOADS", "uploads/");
  6. // editor file name
  7. define("EDITOR", "editor.php");
  8. // website url, where the editor is
  9. define("DEV_WEBSITE_URL", "http://platform.3tierlogic.com/tabbuilder1_3/editor/");
  10. // max size of the uploaded files, currently - 1Mb
  11. define("MAX_FILE_SIZE", 1024 * 1024 * 1 );
  12. // users with these ids are admins (get ids from the users table)
  13. $arrayAdmins = array( 1, 2 ); // don't forget to remove "2"
  14. // tabs per pager limit
  15. define("TABS_PER_PAGE", 10 );
  16. // apps id/secret
  17. $app_details = array(
  18. array( '379739182116899', 'dfa4432ebba89adab0c058ebd9643668' ),
  19. array( '192358084235512', '16b3f9fe38467433aabc5d8abc0e39f5' ),
  20. array( '265487816910972', '040bac342203d77cb3358ec1f208e9f7' ),
  21. array( '522527257771475', '3bfd2f8cd4acaaaa52f10ebbf4accb31' ),
  22. array( '448855071830201', 'ad12a90e0db5825609367a3f9897935b' ),
  23. array( '278867175575177', '1ec16b5a75f92387e6c5eef1b781ecc9' ),
  24. array( '305258642910575', 'dbc1d25a1e2a41f9b6e76d2d0dc29b42' ),
  25. array( '470866732978919', '04dd8f6a3c3a2bed54be14632d0eb74c' ),
  26. array( '522825104424666', 'fffb9111d88a55765515aab5f1a28c5d' ),
  27. array( '208927432578401', '9cd6e62925586c5a3298f9d14aaafa24' )
  28. );
  29. //------------------------ FUNCTIONS ------------------------//
  30. # REDIRECT UNAUTHORIZED USERS TO THE HOMEPAGE
  31. function ars_is_user_authorized() {
  32. if ( ! $_SESSION['user_info'] ) header( 'Location: index.php' );
  33. }
  34. # Check whether the current user has full access
  35. function ars_is_user_superuser() {
  36. if ( isset($_SESSION['superaccount'])) {
  37. return $_SESSION['superaccount'];
  38. } else {
  39. return false;
  40. }
  41. }
  42. # SAVE INFORMATION ABOUT NEW USERS
  43. function ars_save_new_user_info( $user ) {
  44. $query = "SELECT * FROM users WHERE fb_id = '" . $user['id'] . "' ";
  45. $query = mysql_query( $query ) or exit( mysql_error() );
  46. // if we have no records about the user, add it
  47. if ( mysql_num_rows( $query ) == 0 ) {
  48. $query = mysql_query("INSERT INTO users VALUES ( 0,
  49. '".TABS_PER_PAGE."',
  50. '1',
  51. '".$user['id']."',
  52. '".$user['name']."',
  53. '".$user['first_name']."',
  54. '".$user['last_name']."',
  55. '".$user['link']."',
  56. '".$user['username']."',
  57. '".$user['hometown']['name']."',
  58. '".$user['location']['name']."',
  59. '".$user['gender']."',
  60. '".$user['email']."',
  61. '".$user['timezone']."',
  62. '".$user['locale']."',
  63. '".$user['verified']."',
  64. '".$user['updated_time']."'
  65. );") or exit( mysql_error() );
  66. }
  67. } // ars_save_new_user_info() end
  68. # CHECK IF CURRENT USER IS WEBSITE ADMIN
  69. function ars_is_admin( $fb_id ) {
  70. global $arrayAdmins;
  71. $userIsAdmin = false;
  72. $query = "SELECT id FROM users WHERE fb_id = '".$fb_id."' ";
  73. $query = mysql_query( $query ) or exit( mysql_error() );
  74. $user = mysql_fetch_assoc( $query );
  75. for ( $i = 0; $i < count($arrayAdmins); $i++ ) {
  76. if ( $user['id'] == $arrayAdmins[$i] ) {
  77. $userIsAdmin = true;
  78. break;
  79. }
  80. }
  81. return $userIsAdmin;
  82. }
  83. # CHECK IF EDITED PAGE BELONGS TO THE USER
  84. function ars_is_it_my_page( $pageId, $userId ) {
  85. $result = false;
  86. // is user website admin?
  87. if ( ars_is_admin( $userId ) )
  88. $result = true;
  89. // has user fb admin rights for current tab?
  90. else {
  91. $user_pages = $_SESSION['user_pages'];
  92. for ( $i = 0; $i < count($user_pages); $i++ ) {
  93. $query = "SELECT * FROM pages WHERE fan_page_id = '".$user_pages[$i]['id']."' ";
  94. $query = mysql_query( $query ) or exit( mysql_error() );
  95. if ( mysql_num_rows( $query ) > 0 ) {
  96. while ( $tab = mysql_fetch_assoc( $query ) ) {
  97. if ( $tab['id'] == $pageId ) {
  98. $result = true;
  99. break;
  100. }
  101. }
  102. } // endif
  103. } // endfor
  104. } // user has fb admin rights for current page
  105. if ( $result == false ) header( 'Location: dashboard.php' );
  106. } // ars_is_it_my_page() end
  107. # SHORTEN TAB NAME (EDITOR - PREVIEW)
  108. function shortName( $name, $chars = 10 ) {
  109. if ( strlen($name) > $chars ) {
  110. // search spaces among the first "$chars" symbols
  111. $cut = ( strpos( $str, ' ' ) !== false && strpos( $str, ' ' ) < $chars ? false : true );
  112. $name = wordwrap( $name, $chars, ';;', $cut );
  113. $name = explode( ';;', $name );
  114. $name = $name[0] . '...';
  115. }
  116. return $name;
  117. }
  118. # ESCAPE AGAINST SQL INJECTION
  119. function escapeStr( $str ) {
  120. return mysql_real_escape_string( trim($str) );
  121. }
  122. # PAGE INFO
  123. // if $pageID is undefined, function will return the page with the biggest id (latest added)
  124. function ars_getPageInfo( $pageID = '' ) {
  125. $where = $pageID == '' ? '' : "WHERE id = '".$pageID."' ";
  126. $query = "SELECT * FROM pages ".$where." ORDER BY id DESC LIMIT 1";
  127. $query = mysql_query( $query ) or exit( mysql_error() );
  128. $page = mysql_fetch_assoc( $query );
  129. return $page;
  130. }
  131. # GET PAGE NAME BY ID
  132. function ars_get_page_name_by_id( $id ) {
  133. $user_pages = $_SESSION['user_pages'];
  134. for ( $i = 0; $i < count($user_pages); $i++ ) {
  135. if ( $user_pages[$i]['id'] == $id ) $page_name = $user_pages[$i]['name'];
  136. }
  137. return $page_name;
  138. }
  139. # GET CONTEST DESCRIPTION BY CAMPAIGN ID
  140. function getContestDescription( $id ) {
  141. $query = "SELECT campaign_description FROM sp_campaign_info WHERE campaign_sid = '".$id."' LIMIT 1";
  142. $query = mysql_query( $query ) or exit( mysql_error() );
  143. return mysql_result( $query, 0 );
  144. }
  145. # REMOVE SPACES
  146. function removeSpaces( $str ) {
  147. return str_replace( ' ', '-', $str );
  148. }
  149. # CHECK UPLOADED FILE FOR ERRORS
  150. function ars_check_file( $inputName ) {
  151. $error = false;
  152. // check extension
  153. $allow = array( 'jpeg', 'jpg', 'png', 'gif' );
  154. $myExt = pathinfo( $_FILES[$inputName]['name'], PATHINFO_EXTENSION );
  155. if ( ! in_array( $myExt, $allow ) )
  156. $error = 'ERROR_Only jpg, png and gif files are allowed';
  157. // check size
  158. elseif ( $_FILES[$inputName]['size'] > MAX_FILE_SIZE )
  159. $error = 'ERROR_Max file size is ' . MAX_FILE_SIZE / 1024 / 1024 . ' Mb';
  160. return $error;
  161. } // ars_check_file()
  162. # AVAILABLE WIDGET INFO
  163. // if $pageID is undefined, function will return the page with the biggest id (latest added)
  164. function ars_getWidgetsInfo( $partnerID ) {
  165. $where = "WHERE partner_sid = '".$partnerID."' ";
  166. $query = "SELECT * FROM sp_partner_widgets ".$where." LIMIT 1";
  167. $query = mysql_query( $query ) or exit( mysql_error() );
  168. $widgets = mysql_fetch_assoc( $query );
  169. return $widgets;
  170. }
  171. # DECODE URL
  172. function ars_urldecode( $string ) {
  173. $string = str_replace( "%3C", "<", $string );
  174. $string = str_replace( "%3E", ">", $string );
  175. $string = str_replace( "%20", " ", $string );
  176. $string = str_replace( "%0A", " ", $string );
  177. $string = str_replace( "%0D", " ", $string );
  178. $string = str_replace( "%3D", "=", $string );
  179. $string = str_replace( "%27", "\'", $string );
  180. $string = str_replace( "%22", "\"", $string );
  181. $string = str_replace( "%3A", ":", $string );
  182. $string = str_replace( "%3B", ";", $string );
  183. $string = str_replace( "%2F", "/", $string );
  184. $string = str_replace( "%21", "!", $string );
  185. $string = str_replace( "%23", "#", $string );
  186. $string = str_replace( "%7B", "{", $string );
  187. $string = str_replace( "%7D", "}", $string );
  188. $string = str_replace( "%28", "(", $string );
  189. $string = str_replace( "%29", ")", $string );
  190. $string = str_replace( "%3F", "?", $string );
  191. $string = str_replace( "%2C", ",", $string );
  192. $string = str_replace( "%26", "&", $string );
  193. $string = str_replace( "%25", "%", $string );
  194. $string = str_replace( "%24", "$", $string );
  195. $string = str_replace( "%09", "\t", $string );
  196. return $string;
  197. }
  198. //------------------------- FACEBOOK ------------------------//
  199. # GET SECRET BY APP ID
  200. function ars_get_secret_by_app_id( $appId, $app_details ) {
  201. for ( $i = 0; $i < count($app_details); $i++ ) {
  202. if ( $app_details[$i][0] == $appId ) return $app_details[$i][1];
  203. }
  204. }
  205. # GET FREE APP ID FOR TAB INSTALLATION
  206. function ars_get_free_app_id( $pageId, $app_details ) {
  207. $appId;
  208. // get all used apps ids
  209. $query = "SELECT app_id FROM pages WHERE fan_page_id = '".$pageId."' ";
  210. $query = mysql_query( $query ) or exit( mysql_error() );
  211. // user has already created tabs...
  212. if ( mysql_num_rows( $query ) > 0 ) {
  213. // make array of all used apps
  214. $usedApps = array();
  215. while ( $app = mysql_fetch_assoc( $query ) ) {
  216. array_push( $usedApps, $app['app_id'] );
  217. }
  218. // make array of all existing apps
  219. $existingApps = array();
  220. for ( $i = 0; $i < count($app_details); $i++ ) {
  221. array_push( $existingApps, $app_details[$i][0] );
  222. }
  223. // comare arrays
  224. $freeApps = array_diff( $existingApps, $usedApps );
  225. $appId = array_shift( array_values($freeApps) );
  226. }
  227. // no result, return the first app id in array
  228. else {
  229. $appId = $app_details[0][0];
  230. }
  231. return $appId;
  232. } // ars_get_free_app_id() end
  233. ########### DELETE PAGES AND / OR FILES ############
  234. /* arguments:
  235. * $id the id of the page (file) to delete
  236. * $rel if = 'id', deletes itelf; if = 'parent', deletes all children elements
  237. * $whereSpec additional conditions for delete operation
  238. */
  239. function ars_delete( $id, $rel = 'id', $whereSpec = '' ) {
  240. // if can't delete, return false
  241. $result = false;
  242. $whereSpec = $whereSpec == '' ? '' : "AND ".$whereSpec;
  243. // delete files, if any
  244. $query = "SELECT name FROM pages WHERE ".$rel." = '".$id."' ".$whereSpec;
  245. $query = mysql_query( $query ) or exit( mysql_error() );
  246. if ( mysql_num_rows( $query ) > 0 ) {
  247. while ( $file = mysql_fetch_assoc( $query ) ) {
  248. $path = DIR_UPLOADS . $file['name'];
  249. if ( file_exists ( $path ) ) unlink ( DIR_UPLOADS . $file['name'] );
  250. }
  251. }
  252. // delete record(s) from db
  253. $query = "DELETE FROM pages WHERE ".$rel." = '".$id."' ".$whereSpec;
  254. $query = mysql_query( $query ) or exit( mysql_error() );
  255. if ( $query ) $result = true;
  256. return $result;
  257. } // ars_delete() end
  258. ################## FILE UPLOAD ##################
  259. /* arguments:
  260. * $inputName the name of the file input, which is used for sending
  261. * $parentID the id of the parent page
  262. * $type the type (role) of the uploaded image
  263. */
  264. function ars_uploadFile( $inputName, $parentID, $type, $fbUserId = 0 ) {
  265. $uploadedFileName = '';
  266. // file was sent?
  267. if ( $_FILES[$inputName]['size'] != 0 ) {
  268. // check for errors
  269. $error = ars_check_file( $inputName );
  270. if ( $error ) { return $error; exit(); }
  271. $filename = $_FILES[$inputName]['name'];
  272. $tmp_filename = $_FILES[$inputName]['tmp_name'];
  273. // if there're spaces in the file name, replace them by valid symbols
  274. $filename = str_replace( ' ', '-', $filename );
  275. // if there's a file with the same name, as our file has
  276. $newFilename = $parentID . '_' . $filename;
  277. $query = "SELECT id FROM pages WHERE name = '".$newFilename."' ";
  278. $query = mysql_query( $query ) or exit( mysql_error() );
  279. if ( mysql_num_rows( $query ) > 0 ) $newFilename = $parentID . '_' . mt_rand() . '_' . $filename;
  280. // ...upload the file
  281. if ( move_uploaded_file( $tmp_filename, DIR_UPLOADS . $newFilename ) )
  282. $uploadedFileName = $newFilename;
  283. else exit('Error when uploading the file');
  284. // if file was uploaded, record it in db
  285. if ( $uploadedFileName != '' ) {
  286. $query = mysql_query("INSERT INTO pages VALUES ( 0,
  287. '".$fbUserId."',
  288. '',
  289. '',
  290. '".$parentID."',
  291. '".$type."',
  292. '".$uploadedFileName."',
  293. '', '', '', '', '', '', '', '0', '0', '0', '0'
  294. );") or exit( mysql_error() );
  295. } // file was uploaded
  296. } // file was sent
  297. return $uploadedFileName;
  298. } // ars_uploadFile() end
  299. ################## FILE UPDATE ##################
  300. /* arguments:
  301. * $inputName the name of the file input, which is used for sending
  302. * $id the id of the image, which has to be updated
  303. */
  304. function ars_updateFile( $inputName, $id ) {
  305. $uploadedFileName = '';
  306. // file was sent?
  307. if ( $_FILES[$inputName]['size'] != 0 ) {
  308. // check for errors
  309. $error = ars_check_file( $inputName );
  310. if ( $error ) { return $error; exit(); }
  311. $filename = $_FILES[$inputName]['name'];
  312. $tmp_filename = $_FILES[$inputName]['tmp_name'];
  313. // if there're spaces in the file name, replace them by valid symbols
  314. $filename = str_replace( ' ', '-', $filename );
  315. // get old name
  316. $query = "SELECT name FROM pages WHERE id = '".$id."' ";
  317. $query = mysql_query( $query ) or exit( mysql_error() );
  318. $oldFilename = mysql_result( $query, 0 );
  319. // delete old file
  320. $path = DIR_UPLOADS . $oldFilename;
  321. if ( file_exists ( $path ) ) unlink ( DIR_UPLOADS . $oldFilename );
  322. // create new file name
  323. $parentID = substr( $oldFilename, 0, strpos( $oldFilename, '_' ) );
  324. $newFilename = $parentID . '_' . $filename;
  325. // if there's a file with the same name, as our file has
  326. $query = "SELECT id FROM pages WHERE name = '" . $newFilename . "' ";
  327. $query = mysql_query( $query ) or exit( mysql_error() );
  328. if ( mysql_num_rows( $query ) > 0 ) $newFilename = $parentID . '_' . mt_rand() . '_' . $filename;
  329. // upload new file
  330. if ( move_uploaded_file( $tmp_filename, DIR_UPLOADS . $newFilename ) )
  331. $uploadedFileName = $newFilename;
  332. else exit('Error when uploading the file');
  333. // if file was uploaded, update the record in db
  334. if ( $uploadedFileName != '' ) {
  335. mysql_query("UPDATE pages SET
  336. name = '".$uploadedFileName."'
  337. WHERE id = '".$id."'
  338. ") or exit( mysql_error() );
  339. } // file was uploaded
  340. } // file was sent
  341. return $uploadedFileName;
  342. } // ars_updateFile() end
  343. ############## DELETE UNUSED FILES ###############
  344. /* arguments:
  345. * $pageId the id of the page, which is to be ckecked for unused files
  346. */
  347. function ars_deleteUnusedFiles( $pageId ) {
  348. // get content
  349. $query = "SELECT * FROM pages WHERE id='".$pageId."' LIMIT 1 ";
  350. $query = mysql_query( $query ) or exit( mysql_error() );
  351. $content = mysql_fetch_assoc( $query );
  352. $cNonFans = $content['content_nonfans'];
  353. $cFans = $content['content_fans'];
  354. $cContestFans = $content['content_contest_fans'];
  355. $cContestForm = $content['content_contest_form'];
  356. $cContestUGCForm = $content['content_contest_ugc_form'];
  357. $cContestUGCGallery = $content['content_contest_ugc_gallery'];
  358. $cContestThank = $content['content_contest_thank'];
  359. // get files to be ckecked
  360. $query = "SELECT name FROM pages WHERE parent_id = '".$pageId."' AND saved = '0' ";
  361. $query = mysql_query( $query ) or exit( mysql_error() );
  362. if ( mysql_num_rows( $query ) > 0 ) {
  363. while ( $file = mysql_fetch_assoc( $query ) ) {
  364. if ( ! strpos( $cNonFans, $file['name'] ) && ! strpos( $cFans, $file['name'] ) && ! strpos( $cContestFans, $file['name'] ) && ! strpos( $cContestForm, $file['name'] ) && ! strpos( $cContestUGCForm, $file['name'] ) && ! strpos( $cContestUGCGallery, $file['name'] ) && ! strpos( $cContestThank, $file['name'] ) ) {
  365. // delete file
  366. $path = DIR_UPLOADS . $file['name'];
  367. if ( file_exists ( $path ) ) unlink ( DIR_UPLOADS . $file['name'] );
  368. // delete record from db
  369. $query2 = "DELETE FROM pages WHERE name = '".$file['name']."' ";
  370. $query2 = mysql_query( $query2 ) or exit( mysql_error() );
  371. } // endif
  372. } // endwhile
  373. } // endif
  374. } // ars_deleteUnusedFiles() end
  375. ############# DASHBOARD - SHOW PAGES ############
  376. function ars_show_user_pages( $pages, $currentPageId = 0 ) {
  377. if ( count($pages) > 0 ) :
  378. // choose only pages, which have tabs
  379. $pagesWithTabs = array();
  380. for ( $i = 0; $i < count($pages); $i++ ) {
  381. $query = "SELECT * FROM pages WHERE type = 'tab' AND fan_page_id = '".$pages[$i]['id']."' ";
  382. $query = mysql_query( $query ) or exit( mysql_error() );
  383. if ( mysql_num_rows( $query ) > 0 ) {
  384. array_push( $pagesWithTabs, array( $pages[$i]['id'], $pages[$i]['name'] ) );
  385. }
  386. }
  387. // if we have pages with tabs
  388. if ( count($pagesWithTabs) > 0 ) : ?>
  389. <div class="userPages">
  390. <ul>
  391. <li><a href="dashboard.php" <?php if ( ! $currentPageId ) echo 'class="active"'; ?>>All tabs</a></li>
  392. <?php for ( $i = 0; $i < count($pagesWithTabs); $i++ ) : ?>
  393. <li><a href="dashboard.php?id=<?php echo $pagesWithTabs[$i][0]; ?>"
  394. <?php if ( $currentPageId == $pagesWithTabs[$i][0] ) echo 'class="active"'; ?>>
  395. <?php echo $pagesWithTabs[$i][1]; ?></a>
  396. </li>
  397. <?php endfor; ?>
  398. </ul>
  399. </div><!-- / .userPages -->
  400. <?php else : echo "<p>No pages with tabs found</p>"; ?>
  401. <?php endif; ?>
  402. <?php else : echo "<p>You have no created pages on Facebook</p>"; ?>
  403. <?php endif;
  404. } // ars_show_user_pages() end
  405. # DASHBOARD - SHOW USER TABS (AND TABS, WHERE HE IS ADMIN)
  406. function ars_get_allowed_tabs( $pages, $id = 0, $searchWord = '' ) {
  407. $query = "";
  408. // show tabs only from the chosen page
  409. if ( $id ) {
  410. $query .= "SELECT * FROM pages WHERE type = 'tab' AND fan_page_id = '".$id."' ";
  411. }
  412. // show tabs that match to $searchWord
  413. elseif ( $searchWord ) {
  414. for ( $i = 0; $i < count($pages); $i++ ) {
  415. $query .= "SELECT * FROM pages WHERE type = 'tab' AND fan_page_id = '".$pages[$i]['id']."'
  416. AND LOWER(name) LIKE '%".$searchWord."%' ";
  417. if ( count($pages) - $i > 1 ) $query .= "UNION ";
  418. }
  419. }
  420. // show tabs from all pages
  421. else {
  422. for ( $i = 0; $i < count($pages); $i++ ) {
  423. $query .= "SELECT * FROM pages WHERE type = 'tab' AND fan_page_id = '".$pages[$i]['id']."' ";
  424. if ( count($pages) - $i > 1 ) $query .= "UNION ";
  425. }
  426. }
  427. $query = mysql_query( $query ) or exit( mysql_error() );
  428. return $query;
  429. }
  430. ############ FILTER APPS FROM USER PAGES ##########
  431. function ars_filter_apps_from_pages( $arr ) {
  432. foreach ( $arr['data'] as $key => $value ) {
  433. if ( $arr['data'][$key]['category'] == 'Application' ) {
  434. unset( $arr['data'][$key] );
  435. }
  436. }
  437. // new array keys
  438. $arr = array_values( $arr );
  439. return $arr[0];
  440. }
  441. ############### CREATE GRID IMAGES ##############
  442. // run this function once to create background images for grid widget
  443. function create_grid_images() {
  444. $gridSizeMin = 1;
  445. $gridSizeMax = 810;
  446. $gridPath = 'images/grid';
  447. // if no grid folder exist
  448. if ( ! is_dir($gridPath) ) {
  449. // create folder
  450. mkdir($gridPath);
  451. // create grid images
  452. for ( $i = $gridSizeMin; $i <= $gridSizeMax; $i++ ) {
  453. // create canvas
  454. $canvas = imagecreatetruecolor( $i, $i );
  455. imagesavealpha( $canvas, true );
  456. $transparentBgColor = imagecolorallocatealpha( $canvas, 0, 0, 0, 127 );
  457. imagefill( $canvas, 0, 0, $transparentBgColor );
  458. // create border color
  459. $gridColor = imagecolorallocate( $canvas, 153, 153, 153 ); // #999
  460. // draw left border
  461. imageline( $canvas, 0, 0, 0, $i - 1, $gridColor );
  462. // draw top border
  463. imageline( $canvas, 0, 0, $i - 1, 0, $gridColor );
  464. // save grid image
  465. $filePath = $gridPath . '/grid-' . $i . '.png';
  466. imagepng( $canvas, $filePath );
  467. } // endfor
  468. } // endif
  469. } // create_grid_images()
  470. ################## UGC GALLERY #################
  471. // auxiliary function
  472. function getImgUgcSidAndCampaignSid( $name, $resultToReturn ) {
  473. $query = "SELECT * FROM sp_campaign_ugc WHERE ugc_photo_name = '".$name."' LIMIT 1";
  474. $query = mysql_query( $query ) or exit( mysql_error() );
  475. $query = mysql_fetch_assoc( $query );
  476. // return ugc_sid
  477. if ( $resultToReturn == 'img' )
  478. return $query['ugc_sid'];
  479. // return ugc_campaign_sid
  480. else // ( $resultToReturn == 'campaign' )
  481. return $query['ugc_campaign_sid'];
  482. }
  483. // check if a user can vote for an image
  484. function checkVotingAbility( $campaign_id, $fb_user_id, $img_ugc_sid ) {
  485. // get voting start-end dates and frequency
  486. $query = "SELECT * FROM sp_campaign_info WHERE campaign_sid = '".$campaign_id."' LIMIT 1 ";
  487. $query = mysql_query( $query ) or exit( mysql_error() );
  488. $voting = mysql_fetch_assoc( $query );
  489. $votingStart = $voting['campaign_voting_start_time'];
  490. $votingEnd = $voting['campaign_voting_end_time'];
  491. $votingFrequency = $voting['campaign_vote_frequency'];
  492. // vars for allowing
  493. $votingAbility = $allow_start = $allow_end = $allow_frequency = false;
  494. // start date
  495. if ( strtotime( $votingStart ) < time() ) {
  496. $allow_start = true;
  497. } else {
  498. $votingAbility = 'Voting starts on ' . date( 'M d, Y', strtotime( $votingStart ) ) . '.';
  499. }
  500. // end date
  501. if ( strtotime( $votingEnd ) > time() ) {
  502. $allow_end = true;
  503. } else {
  504. $votingAbility = 'Voting has ended.';
  505. }
  506. // frequency
  507. // unlimited times per campaign, but only once per any image
  508. if ( $votingFrequency == '-1' ) {
  509. $query = "SELECT * FROM sp_campaign_ugc_vote WHERE vc_facebookid = '".$fb_user_id."'
  510. AND vc_contentid = '".$img_ugc_sid."' ";
  511. $query = mysql_query( $query ) or exit( mysql_error() );
  512. $votesPerImage = mysql_num_rows( $query );
  513. if ( $votesPerImage == 0 ) $allow_frequency = true;
  514. else $votingAbility = 'You can vote only once per image.';
  515. }
  516. // once per campaign
  517. elseif ( $votingFrequency == '0' ) {
  518. $query = "SELECT * FROM sp_campaign_ugc_vote WHERE vc_facebookid = '".$fb_user_id."'
  519. AND vc_campaign_sid = '".$campaign_id."' ";
  520. $query = mysql_query( $query ) or exit( mysql_error() );
  521. $votesPerCampaign = mysql_num_rows( $query );
  522. if ( $votesPerCampaign == 0 ) $allow_frequency = true;
  523. else $votingAbility = 'You can vote only once per campaign.';
  524. }
  525. // one or more times per day
  526. else {
  527. // get user's votes during the last 24 hours
  528. $query = "SELECT * FROM sp_campaign_ugc_vote WHERE vc_facebookid = '".$fb_user_id."'
  529. AND vc_campaign_sid = '".$campaign_id."' AND vc_voteTime > ( NOW() - INTERVAL 1 DAY )";
  530. $query = mysql_query( $query ) or exit( mysql_error() );
  531. $votesDuring24Hours = mysql_num_rows( $query );
  532. // check if a user has voted for an image during the last 24 hours
  533. $query = "SELECT * FROM sp_campaign_ugc_vote WHERE vc_facebookid = '".$fb_user_id."'
  534. AND vc_contentid = '".$img_ugc_sid."' AND vc_voteTime > ( NOW() - INTERVAL 1 DAY )";
  535. $query = mysql_query( $query ) or exit( mysql_error() );
  536. $votesForImageDuring24Hours = mysql_num_rows( $query );
  537. if ( $votesDuring24Hours >= $votingFrequency )
  538. $votingAbility = 'You can vote up to ' . $votingFrequency . ' times per day.';
  539. elseif ( $votesForImageDuring24Hours > 0 )
  540. $votingAbility = 'You have already voted for this image today.';
  541. else $allow_frequency = true;
  542. /* $votes (24 hours) < frequency && $votes( img ) == 0 */
  543. // ORDER BY vc_voteTime DESC
  544. /*
  545. if ( $votesPerCampaign < $votingFrequency )
  546. else {
  547. if ( $votesPerImage == 0 ) $allow_frequency = true;
  548. else {
  549. if ( $votesPerImage == 1 )
  550. $i = 0;
  551. while ( $vote = mysql_fetch_assoc( $votesLatest ) ) {
  552. // we have to check only one vote: the last one - $votingFrequency
  553. if ( $i == $votingFrequency - 1 ) {
  554. if ( time() - 24 * 60 * 60 < strtotime( $vote['vc_voteTime'] ) ) {
  555. $votingAbility = 'You can vote up to ' . $votingFrequency . ' times per day.';
  556. } else {
  557. $votingAbility = 'You have already voted for this image today.';
  558. }
  559. break;
  560. }
  561. $i++;
  562. } // endwhile
  563. }
  564. }
  565. */
  566. } // one or more times per day
  567. if ( $allow_start && $allow_end && $allow_frequency ) $votingAbility = true;
  568. return $votingAbility;
  569. } // checkVotingAbility()
  570. // prepare images for showing
  571. function prepareUgcGalleryImages( $campaign_id, $fb_user_id ) {
  572. $result = '';
  573. // get all ugc contest images
  574. $query = "SELECT * FROM sp_campaign_ugc WHERE ugc_campaign_sid = '".$campaign_id."'
  575. AND ugc_approve_state=1
  576. ORDER BY ugc_upload_time DESC";
  577. $query = mysql_query( $query ) or exit( mysql_error() );
  578. if ( mysql_num_rows( $query ) == 0 ) {
  579. $result = '<p>This campaign has no contest images yet.</p>';
  580. } else {
  581. while ( $img = mysql_fetch_assoc( $query ) ) {
  582. // get user name
  583. $query1 = "SELECT * FROM sp_campaign_result
  584. WHERE cpresult_sid = '".$img['ugc_user_id']."' LIMIT 1";
  585. $query1 = mysql_query( $query1 ) or exit( mysql_error() );
  586. $user = mysql_fetch_assoc( $query1 );
  587. $userName = $user['cpresult_first_name'];
  588. $userSurname = $user['cpresult_last_name'];
  589. if ( $userSurname != '' ) $userSurname = substr( $userSurname, 0, 1 ) . '.';
  590. $userFullName = $userName . ' ' . $userSurname;
  591. // generate content for ugc gallery
  592. $imgUrl = DEV_WEBSITE_URL . DIR_UPLOADS . $img['ugc_photo_name'];
  593. $result .= '<li>';
  594. $result .= '<div class="pic"><img src="'. $imgUrl . '" alt="" /><span></span></div>';
  595. $result .= '<a class="aButton view">View</a>';
  596. // check if a user can vote for an image
  597. $img_ugc_sid = getImgUgcSidAndCampaignSid( $img['ugc_photo_name'], 'img' );
  598. if ( checkVotingAbility( $campaign_id, $fb_user_id, $img_ugc_sid ) === true ) {
  599. $result .= '<a class="aButton vote">Vote</a>';
  600. } else {
  601. $result .= '<a class="aButton vote clicked">Vote</a>';
  602. }
  603. $result .= '<div class="text">';
  604. $result .= '<span class="title">' . shortName( $img['ugc_title'] ) . '</span>';
  605. $result .= '<span class="user">' . $userFullName . '</span>';
  606. $result .= '<span class="desc">' . nl2br( shortName( $img['ugc_description'], 40 ) ) . '</span>';
  607. $result .= '</div>';
  608. $result .= '</li>';
  609. }
  610. }
  611. return $result;
  612. } // prepareUgcGalleryImages()
  613. // документ UTF-8
  614. ?>