public static function save() { if (isset($_POST) && isset($_POST['error']) || !isset($_POST['access_token'])) { if (isset($_POST['error']) && $_POST['error'] != 'immediate_failed') { \Util::notice(array('type' => 'danger', 'text' => 'Sorry, operation failed with the following error: ' . $_POST['error'])); } else { echo json_encode(array('error' => 'immediate_failed')); } } else { $share = SharesModel::first(array('link' => $_POST['task'])); if ($share) { $share = $share->toArray(); $data = json_decode($share['data'], true); if (strtotime($share['created_at']) + $share['expires'] > time()) { $task = TasksModel::first($share['task_id'])->toArray(); $service = ServicesModel::first($share['service_id'])->toArray(); $source = UsersModel::profile($task['user_id']); $destination['username']['id'] = uniqid(); $destination['credentials'] = $_POST; call_user_func_array(array('app\\libraries\\' . $service['library'], 'backup'), array($destination, 0, $task['id'])); call_user_func_array(array('app\\libraries\\' . $service['library'], 'migrate'), array($source, $destination, $task['id'], false, $data)); \Util::notice(array('type' => 'success', 'text' => 'The data is being imported. Please check your account in a couple of minutes.')); } else { \Util::notice(array('type' => 'danger', 'text' => 'The requested link has expired.')); } } } }
/** * Dashboard index page */ public static function index($finish = false) { if ($finish) { \Auth::showWizard('finish'); } else { if (\Auth::showWizard()) { \Router::redirect('accounts/permissions'); } } $templateData = array('template' => 'dashboard/content', 'title' => 'Migrate - Dashboard', 'bodyId' => 'dashboard', 'styles' => array('dashboard.css'), 'scripts' => array(), 'queue' => TasksModel::listingFor(array('user_id' => $_SESSION['current']['username']['id'], 'user_affected_id' => $_SESSION['current']['username']['id']), 4), 'services' => ServicesModel::forUser(array('id' => $_SESSION['current']['username']['id'], 'limit' => 4)), 'usernames' => $_SESSION['usernames'], 'intro' => (bool) IntroModel::first(array('page' => 'dashboard', 'group' => $_SESSION['current']['username']['group']))); if (!$templateData['intro']) { IntroModel::create(array('page' => 'dashboard', 'group' => $_SESSION['current']['username']['group']))->save(); } \Render::layout('template', $templateData); }
public static function withCredentials() { $usernames = static::all()->toArray(); if ($usernames) { foreach ($usernames as $key => $user) { $username = static::first($user['id'])->toArray(); $userProfile = UsersProfilesModel::first(array('user_id' => $user['id'])); if ($userProfile) { $userProfile = $userProfile->toArray(); } $credentials = UsersCredentialsModel::first(array('user_id' => $user['id'])); if ($credentials) { $credentials = $credentials->toArray(); } $services = ServicesModel::forUser(array('id' => $user['id'])); $usernames[$key] = compact('username', 'userProfile', 'credentials', 'services'); } } return $usernames; }
public static function permissions() { $templateData = array('template' => 'accounts/permissions', 'title' => 'Migrate - Permissions', 'bodyId' => 'accounts-update', 'styles' => array('accounts.css'), 'scripts' => array('accounts.js'), 'heading' => 'Select your services', 'description' => 'These are the current available services that can be used in our operations. You can select any time which services are we allowed to use. If you roll-over a service name you can see all the permissions and what action can we performe.', 'services' => ServicesModel::withPermissions(), 'servicesSoon' => ServicesModel::all(array('status' => ServicesModel::STATUS_SOON))->toArray(), 'selectedServices' => UsersServicesModel::all(array('user_id' => $_SESSION['current']['username']['id']))->column('service_id'), 'skipUrl' => 'accounts/add', 'step' => 1); \Render::layout('template', $templateData); }
public static function _clean($destination, $source, $syncTaskId = 0, $whitelist = array()) { $service = ServicesModel::first(array('library' => 'Tasks'))->toArray(); $task = TasksServicesModel::first(array('task_id' => $syncTaskId, 'service_id' => $service['id']))->toArray(); $task['stats'] = json_decode($task['stats'], true); // lists $lists = BackupsModel::all(array('user_id' => $source['username']['id'], 'sync_task_id' => $syncTaskId, 'entity_type' => static::$kind['list']))->toArray(); if ($lists && ($task['stats']['lists'] || $destination['username']['id'] == $source['username']['id'])) { foreach ($lists as $list) { // $whitelist - shared or clean data if ($whitelist) { if (isset($whitelist['lists']) && in_array($list['entity_id'], $whitelist['lists'])) { \Rest::delete('https://www.googleapis.com/tasks/v1/users/@me/lists/' . $list['entity_id'], array(), $destination); } } else { if ($destination['username']['id'] == $source['username']['id']) { \Rest::delete('https://www.googleapis.com/tasks/v1/users/@me/lists/' . $list['entity_id'], array(), $destination); } else { if ($list['entity_new_id']) { \Rest::delete('https://www.googleapis.com/tasks/v1/users/@me/lists/' . $list['entity_new_id'], array(), $destination); } } } } } }
public static function _clean($destination, $source, $syncTaskId = 0, $whitelist = array()) { $service = ServicesModel::first(array('library' => 'Calendar'))->toArray(); $task = TasksServicesModel::first(array('task_id' => $syncTaskId, 'service_id' => $service['id']))->toArray(); $task['stats'] = json_decode($task['stats'], true); // Calendars $calendars = CalendarsModel::all(array('user_id' => $source['username']['id'], 'sync_task_id' => $syncTaskId))->toArray(); // Events $toDeleteEvents = array(); $migratedEvents = MigratedDataModel::all(array('source_id' => $source['username']['id'], 'sync_task_id' => $syncTaskId, 'kind' => static::$kind['event'])); if ($migratedEvents) { $migratedEvents = $migratedEvents->toArray(); if ($migratedEvents) { $destinationCalendars = CalendarsModel::all(array('user_id' => $destination['username']['id'], 'sync_task_id' => $syncTaskId)); if ($destinationCalendars) { $destinationCalendars = $destinationCalendars->toArray(); if ($destinationCalendars) { foreach ($destinationCalendars as $calendar) { // get events $newEvents = array(); do { $payload = array('maxResults' => static::$limit); if (isset($events['nextPageToken'])) { $payload['pageToken'] = $events['nextPageToken']; } $events = \Rest::get(sprintf(static::$endpoints['events'], $calendar['calendar_id']), $payload, $destination); if (isset($events['result']['error'])) { d(static::$endpoints['events']); } if (isset($events['items'])) { foreach ($events['items'] as $event) { if (isset($event['summary'])) { $newEvents[static::identifier($event, $calendar)] = $event['id']; } } } } while (isset($events['nextPageToken']) && $events['nextPageToken']); // Mark events foreach ($migratedEvents as $event) { if (isset($newEvents[$event['unique']])) { $toDeleteEvents[] = array('eventId' => $newEvents[$event['unique']], 'calendarId' => $calendar['calendar_id']); } } } } } } } switch (true) { # Share / Clean ############################################################################ case !empty($whitelist): // Calendars if ($calendars) { foreach ($calendars as $calendar) { if (isset($whitelist['calendars']) && in_array($calendar['calendar_id'], $whitelist['calendars'])) { \Rest::delete('https://www.googleapis.com/calendar/v3/calendars/' . $calendar['calendar_id'], array(), $destination); } } } break; # Move ############################################################################ # Move ############################################################################ case $destination['username']['id'] == $source['username']['id']: // Calendars if ($calendars) { foreach ($calendars as $calendar) { \Rest::delete('https://www.googleapis.com/calendar/v3/calendars/' . $calendar['calendar_id'], array(), $destination); } } # Migrate / Sync ############################################################################ # Migrate / Sync ############################################################################ default: // Calendars if ($calendars && $task['stats']['calendars']) { foreach ($calendars as $calendar) { if ($calendar['calendar_new_id']) { \Rest::delete('https://www.googleapis.com/calendar/v3/calendars/' . $calendar['calendar_new_id'], array(), $destination); } } } // Events if ($toDeleteEvents && $task['stats']['events']) { foreach ($toDeleteEvents as $event) { \Rest::delete('https://www.googleapis.com/calendar/v3/calendars/' . $event['calendarId'] . '/events/' . $event['eventId'], array(), $destination); } } break; } }
public static function oAuthTokensLogin($tokens = false) { $profile = Rest::get('https://www.googleapis.com/oauth2/v1/userinfo', array('alt' => 'json', 'access_token' => $tokens['access_token'])); $username = UsersModel::first(array('google_id' => $profile['id'], 'status' => UsersModel::STATUS_ACTIVE)) ?: UsersModel::create(); $redirectUrl = BASE_URL . 'dashboard'; // Existing (if services > 1 refresh token) + update last_login + update credentials if ($username->id) { // Credentials update $credentials = UsersCredentialsModel::first(array('user_id' => $username->id)); // Refresh token if needed $services = ServicesModel::forUser(array('id' => $username->id)); if (count($services) > 1) { $credentialsArray['username']['id'] = $username->id; $credentialsArray['credentials'] = $credentials->toArray(); $tokens = static::oAuthRefreshToken($credentialsArray, false, 'force'); } $credentials->access_token = $tokens['access_token']; $credentials->expires_at = date(DATE_TIME, time() + $tokens['expires_in']); $credentials->save(); $credentials = $credentials->toArray(); // User profile $userProfile = UsersProfilesModel::first(array('user_id' => $username->id))->toArray(); // Username update $username->last_login = date(DATE_TIME); $username->save(); $username = $username->toArray(); $setSession = true; // Session thingie static::setUsername(compact('username', 'services', 'credentials', 'userProfile', 'setSession')); // New User } else { // Get refresh token $data = array('client_id' => OAUTH_CLIENT_ID, 'client_secret' => OAUTH_CLIENT_SECRET, 'redirect_uri' => 'postmessage', 'code' => $tokens['code'], 'grant_type' => 'authorization_code'); $tokens = Rest::post('https://accounts.google.com/o/oauth2/token', $data); $tokens['expires_at'] = date(DATE_TIME, time() + $tokens['expires_in']); $gplus = Rest::get('https://www.googleapis.com/plus/v1/people/me', array(), array('credentials' => $tokens)); $profile = array_merge($profile, $gplus); static::_createUser($profile, $tokens, 'setSession'); } return array('success' => 'true', 'redirectUrl' => $redirectUrl); }
public static function data() { $service = ServicesModel::first($_SESSION['clean']['service'])->toArray(); if (!isset($_SESSION['clean']['data']) || empty($_SESSION['clean']['data'])) { \Auth::oAuthRefreshToken($_SESSION['current'], 'updateSession', 'force'); $_SESSION['clean']['data'] = call_user_func_array(array('app\\libraries\\' . $service['library'], 'share'), array($_SESSION['current'])); } $data = $_SESSION['clean']['data']; echo \Render::view('clean/data', array('data' => $data, 'selectedData' => $_SESSION['clean']['selectedData']), 'return'); }
public static function _clean($destination, $source, $syncTaskId = 0, $whitelist = array()) { $service = ServicesModel::first(array('library' => 'Youtube'))->toArray(); $task = TasksServicesModel::first(array('task_id' => $syncTaskId, 'service_id' => $service['id']))->toArray(); $task['stats'] = json_decode($task['stats'], true); // Playlists $playlists = PlaylistsModel::all(array('user_id' => $source['username']['id'], 'sync_task_id' => $syncTaskId))->toArray(); // Videos $videos = array(); $migratedVideos = MigratedDataModel::all(array('source_id' => $source['username']['id'], 'sync_task_id' => $syncTaskId, 'kind' => static::$kind['video'])); if ($migratedVideos) { $migratedVideos = $migratedVideos->toArray(); if ($migratedVideos) { $destinationPlaylists = PlaylistsModel::all(array('user_id' => $destination['username']['id'], 'sync_task_id' => $syncTaskId)); if ($destinationPlaylists) { $destinationPlaylists = $destinationPlaylists->toArray(); if ($destinationPlaylists) { foreach ($destinationPlaylists as $playlist) { foreach ($migratedVideos as $video) { $payload = array('part' => static::$part['playlistItems']['minimal'], 'playlistId' => $playlist['youtube_playlist_id'], 'videoId' => $video['identifier']); $vids = array(); $vids = \Rest::get(static::$endpoints['playlistItems'], $payload, $destination); if (isset($vids['result']['error'])) { d($vids); } if ($vids && $vids['items']) { foreach ($vids['items'] as $vid) { $videos[] = $vid['id']; } } } } } } } } // Subscriptions $newSubscriptions = array(); $oldSubscriptions = MigratedDataModel::all(array('destination_id' => $destination['username']['id'], 'sync_task_id' => $syncTaskId))->column('identifier'); do { $payload = array('part' => static::$part['subscriptions']['minimal'], 'mine' => 'true', 'maxResults' => static::$limit); if (isset($subscriptions['nextPageToken'])) { $payload['pageToken'] = $subscriptions['nextPageToken']; } $subscriptions = \Rest::get(static::$endpoints['subscriptions'], $payload, $destination); if (isset($subscriptions['result']['error'])) { d($subscriptions); } if ($subscriptions['items']) { foreach ($subscriptions['items'] as $sub) { $newSubscriptions[] = $sub; } } } while (isset($subscriptions['nextPageToken']) && $subscriptions['nextPageToken']); switch (true) { # Share / Clean ############################################################################ case !empty($whitelist): // Playlist if ($playlists) { foreach ($playlists as $playlist) { if (isset($whitelist['playlists']) && in_array($playlist['youtube_playlist_id'], $whitelist['playlists'])) { \Rest::delete(static::$endpoints['playlists'], array('id' => $playlist['youtube_playlist_id']), $destination); } } } // Subscriptions if ($newSubscriptions) { foreach ($newSubscriptions as $sub) { if (isset($whitelist['subscriptions']) && in_array($sub['snippet']['resourceId']['channelId'], $whitelist['subscriptions'])) { \Rest::delete(static::$endpoints['subscriptions'], array('id' => $sub['id']), $destination); } } } break; # Move ############################################################################ # Move ############################################################################ case $destination['username']['id'] == $source['username']['id']: // Playlist if ($playlists) { foreach ($playlists as $playlist) { \Rest::delete(static::$endpoints['playlists'], array('id' => $playlist['youtube_playlist_id']), $destination); } } // Subscriptions if ($newSubscriptions) { foreach ($newSubscriptions as $sub) { \Rest::delete(static::$endpoints['subscriptions'], array('id' => $sub['id']), $destination); } } break; # Migrate / Sync ############################################################################ # Migrate / Sync ############################################################################ default: // Playlist if ($playlists && $task['stats']['playlists']) { foreach ($playlists as $playlist) { if ($playlist['new_youtube_id']) { \Rest::delete(static::$endpoints['playlists'], array('id' => $playlist['new_youtube_id']), $destination); } } } // Videos if ($videos && $task['stats']['videos']) { foreach ($videos as $videoId) { \Rest::delete(static::$endpoints['playlistItems'], array('id' => $videoId), $destination); } } // Subscriptions if ($newSubscriptions) { foreach ($newSubscriptions as $sub) { if (in_array($sub['snippet']['resourceId']['channelId'], $oldSubscriptions) && $task['stats']['subscriptions']) { \Rest::delete(static::$endpoints['subscriptions'], array('id' => $sub['id']), $destination); } } } break; } }
public static function selectService() { $id = (int) $_POST['id']; if ($_POST['action'] == 'select') { $_SESSION['wizard']['services'][$id] = ServicesModel::first($id)->toArray(); } else { if (isset($_SESSION['wizard']['services'][$id])) { unset($_SESSION['wizard']['services'][$id]); } } }