You are here

function brilliant_gallery_pathurl_btsync_check in Brilliant Gallery 7

1 call to brilliant_gallery_pathurl_btsync_check()
replace_brilliant_gallery_tags in ./brilliant_gallery_functions.inc

File

./brilliant_gallery_functions.inc, line 429

Code

function brilliant_gallery_pathurl_btsync_check($pathurl) {

  // Only continue if it looks like a BT Sync secret (must be 33 chars); see http://forum.bittorrent.com/topic/29304-rules-for-valid-secret/
  preg_match("/[A-Z2-7]{33}/", $pathurl, $output_array);
  if (!array_key_exists(0, $output_array)) {
    return FALSE;
  }

  // Only continue if the main gallery folder has been configured.
  if (variable_get('brilliant_gallery_folder', '') == '') {
    return;
  }
  $btsyncdir = 'public://' . variable_get('brilliant_gallery_folder', '') . '/btsync';
  $dirtest = file_prepare_directory($btsyncdir, FILE_CREATE_DIRECTORY);

  // Only continue if the special btsync directory exists and is writable
  if (!$dirtest) {
    watchdog('Brilliant Gal', 'Special folder "btsync" cannot be created or is not writable in ' . ($btsyncdir = 'public://' . variable_get('brilliant_gallery_folder', '')));
    return FALSE;
  }

  // OK now let's check or add the particular sync folder
  $btsyncgallerydir = $btsyncdir . '/' . $pathurl;
  $dirtest = file_prepare_directory($btsyncgallerydir, FILE_CREATE_DIRECTORY);

  // Only continue if the special btsync directory exists and is writable
  if (!$dirtest) {
    watchdog('Brilliant Gal', 'Sync folder "' . $pathurl . '" cannot be created or is not writable in ' . $btsyncdir);
    return FALSE;
  }
  if (function_exists('btsync_method_callback')) {
    btsync_method_callback('add_folder', array(
      'dir' => drupal_realpath($btsyncgallerydir),
      // Did not work without drupal_realpath here.
      'secret' => $pathurl,
    ));
  }
  else {
    watchdog('Brilliant Gal', 'BT Sync gallery formatter selected but the BitTorrent Sync API module ("btsync") is not enabled.');
    return FALSE;
  }
  $pathurl = 'btsync/' . $pathurl;
  return $pathurl;
}