Beispiel #1
0
 public function update()
 {
     $this->output->set_header('Content-Type: application/json; charset=utf-8');
     $this->load->helper('slug');
     $user = $this->ion_auth->user()->row();
     $this->load->library('form_validation');
     if ($this->input->post('song_id')) {
         $song_row = $this->Song_model->get_song($this->input->post('song_id'));
         $real_user_id = $song_row->user_id;
         $real_file_uid = $song_row->file_uid;
     } else {
         redirect('manage/songs', 'refresh');
     }
     if ($this->input->post('user_id') != $user->id && !$this->ion_auth->is_admin() && $this->input->post('song') != $real_file_uid) {
         redirect('manage/songs', 'refresh');
     } else {
         //modal form validaiton
         $this->form_validation->set_rules('artist', 'Artist', 'trim|required|min_length[2]|max_length[100]|xss_clean');
         $this->form_validation->set_rules('title', 'Title', 'trim|required|min_length[2]|max_length[100]|xss_clean');
         $this->form_validation->set_rules('featuring', 'Featuring', 'trim||min_length[2]|max_length[255]|xss_clean');
         $this->form_validation->set_rules('producer', 'Producer', 'trim|min_length[2]|max_length[255]|xss_clean');
         $this->form_validation->set_rules('album', 'Album', 'trim|min_length[2]|max_length[255]|xss_clean');
         $this->form_validation->set_rules('video', 'Video', 'trim|min_length[2]|xss_clean');
         $this->form_validation->set_rules('image', 'Image', 'trim|min_length[2]|xss_clean');
         $this->form_validation->set_rules('description', 'Description', 'trim|min_length[2]|max_length[10000]|xss_clean');
         $this->form_validation->set_rules('song_url', 'Song URL', 'trim|min_length[2]|max_length[255]|xss_clean');
         //hidden inputs
         $file_uid = $this->input->post('song');
         $song_id = $this->input->post('song_id');
         $file_name = $this->input->post('file_name');
         $user_id = $this->input->post('user_id');
         $artist = $this->input->post('artist', TRUE);
         $title = $this->input->post('title', TRUE);
         $featuring = $this->input->post('featuring', TRUE);
         $producer = $this->input->post('producer', TRUE);
         $album = $this->input->post('album', TRUE);
         $video = $this->input->post('video', TRUE);
         $buy_link = $this->input->post('buy_link', TRUE);
         $can_download = $this->input->post('can_download', TRUE);
         $description = $this->input->post('description', TRUE);
         $soundcloud_url = $this->input->post('soundcloud_url', TRUE);
         $published = 'published';
         $song = $this->Song_model->get_song($song_id);
         $username = $song->username;
         $urlslug = $song->song_url;
         $sfname = $song->sfname;
         $visibility = $this->input->post('make_private', TRUE) == 'yes' ? 'unlisted' : 'public';
         // if the user changed the soundcloud URL
         if ($soundcloud_url != $song->external_url) {
             $this->load->helper('soundcloud');
             $sc = json_decode(_resolveSoundcloud($soundcloud_url));
             if (!empty($sc->id)) {
                 $external_file = $sc->id;
             } else {
                 $external_file = $song->external_file;
             }
             $external_url = $soundcloud_url;
         } else {
             $external_file = $song->external_file;
             $external_url = $song->external_url;
         }
         if ($this->input->post('song_url') !== $song->song_url) {
             $urlslug = url_slug($this->input->post('song_url', TRUE));
         }
         //make sure its a youtube video
         if (preg_match('/^(?:https?:\\/\\/)?(?:www\\.)?youtube\\.com\\/watch\\?(?=.*v=((\\w|-){11}))(?:\\S+)?$/', $video)) {
             parse_str(parse_url($video, PHP_URL_QUERY), $my_array_of_vars);
             $video = $my_array_of_vars['v'];
         } else {
             $video = $video;
         }
         if ($this->form_validation->run() == FALSE) {
             //validation errors
             $output_array = array('validation' => 'error', 'message' => validation_errors('<div class="alert alert-error"><strong>Error!</strong> ', '</div>'));
             $this->output->set_output(json_encode($output_array));
         } else {
             if (!$this->Song_model->valid_song_exists(array('file_uid' => $file_uid, 'user_id' => $user_id, 'song_id' => $song_id))) {
                 //couldn't find song that matches the file_uid/user_id/file_name
                 $output_array = array('validation' => 'valid', 'response' => 'error', 'message' => 'It appears the song doesnt exist.');
                 $this->output->set_output(json_encode($output_array));
             } else {
                 if (isset($_FILES['image_file'])) {
                     $uploads = $_FILES['image_file'];
                     if ($uploads['error'] == 0) {
                         $this->load->library('images');
                         $this->images->uploadLocalFile($uploads, $sfname, $urlslug, $user_id);
                         $ext = pathinfo($uploads['name']);
                         $ext = $ext['extension'];
                         $image_dir = FCPATH . 'asset_uploads/' . $song->username . '/' . $urlslug . '/';
                         $size_array = array('64', '150', '300', '500');
                         foreach ($size_array as $size) {
                             $resize = $this->images->resizeImage($image_dir, $sfname, $ext, $size);
                         }
                         if (!$resize) {
                             $output_array = array('validation' => 'valid', 'response' => 'error', 'message' => 'Unable to resize image');
                             $this->output->set_output(json_encode($output_array));
                         }
                         $song_image = $sfname . '.' . $ext;
                     }
                 } else {
                     $song_image = $song->song_image;
                 }
                 $song_data = array('song_artist' => $artist, 'song_title' => $title, 'song_description' => $description, 'featuring' => $featuring, 'song_url' => $urlslug, 'song_producer' => $producer, 'album' => $album, 'file_name' => $file_name, 'video' => $video, 'song_image' => $song_image, 'external_file' => $external_file, 'external_url' => $external_url, 'buy_link' => $buy_link, 'status' => $published, 'visibility' => $visibility, 'can_download' => $can_download);
                 $update_where = array('file_uid' => $file_uid, 'user_id' => $user_id, 'song_id' => $song_id);
                 $song_row_updated = $this->Song_model->update($update_where, $song_data);
                 if (!$song_row_updated) {
                     $output_array = array('validation' => 'valid', 'response' => 'error', 'message' => 'The song was not updated. Did you make any changes?');
                     $this->output->set_output(json_encode($output_array));
                 } else {
                     //paydirt
                     if ($external_file != $song->external_file) {
                         //upload SC Image
                         $this->load->library('images');
                         $this->images->uploadRemoteFile($sc->artwork_url, $sfname, $urlslug);
                         $song_image = $sfname . '.jpg';
                         $image_dir = FCPATH . 'asset_uploads/' . $username . '/' . $urlslug . '/';
                         $size_array = array('64', '150', '300');
                         foreach ($size_array as $size) {
                             $this->images->resizeImage($image_dir, $sfname, 'jpg', $size);
                         }
                     }
                     //used to add more output to the returned json
                     $urlUpdated = NULL;
                     if ($this->input->post('song_url') !== $song->song_url) {
                         $urldata = array('old_url' => $song->song_url, 'current_url' => $urlslug, 'song_id' => $song->song_id, 'date_created' => time());
                         $updateUrl = $this->Song_model->addUpdatedUrl($urldata);
                         if ($updateUrl) {
                             $urlUpdated = "<br /><span style='font-weight:bold;font-size:1em'>The Song URL was updated as well. Please update your links accordingly.</span>";
                         }
                     }
                     //delete existing post cache
                     $sqlWhere = array('username' => $song->username, 'song_url' => $song->song_url);
                     $this->cache->model('Song_model', 'get_song_where', array($sqlWhere), -1);
                     $output_array = array('validation' => 'valid', 'response' => 'success', 'message' => 'Song updated. <a href="' . base_url('song/' . $username . '/' . $urlslug) . '">Click here to view</a>' . $urlUpdated);
                     $this->output->set_output(json_encode($output_array));
                 }
             }
         }
     }
     //end form_validation and JSON returns
 }
Beispiel #2
0
 public function init_soundcloud()
 {
     if (!$this->input->post()) {
         redirect('upload', 'refresh');
     }
     $this->output->set_header('Content-Type: application/json; charset=utf-8');
     $this->load->helper('soundcloud');
     $sc = json_decode(_resolveSoundcloud($this->input->post('scLink')));
     if (empty($sc->id)) {
         $output_array = array('response' => 'error', 'message' => 'Not a valid Soundcloud Link');
     } else {
         $trackNum = $sc->id;
         $artist = substr($sc->title, 0, strpos($sc->title, " - "));
         $title = substr($sc->title, strpos($sc->title, ' - ') + 3);
         // if no soundcloud artwork, use the users avatar
         if (empty($sc->artwork_url)) {
             $scimg = str_replace('large.jpg', 't500x500.jpg', $sc->user->avatar_url);
         } else {
             $scimg = str_replace('large.jpg', 't500x500.jpg', $sc->artwork_url);
         }
         $uid = 'soundcloud_' . md5($sc->title);
         $data = array('user_id' => $this->data['user']->id, 'file_uid' => $uid, 'file_name' => htmlspecialchars($artist . ' - ' . $title, ENT_QUOTES), 'song_artist' => $artist, 'song_title' => $title, 'external_source' => 'soundcloud', 'external_file' => $trackNum, 'external_url' => $this->input->post('scLink'), 'song_image' => $scimg, 'upload_date' => date('Y-m-d H:i:s'), 'status' => 'incomplete');
         if ($sc->streamable != 1) {
             $output_array = array('response' => 'error', 'message' => 'Valid Song but there doesnt seem to be a streamable URL available. Unable to upload. Please try again');
         } else {
             //send data to DB
             $song_id = $this->Song_model->add($data);
             if (!$song_id) {
                 $output_array = array('response' => 'error', 'message' => 'Song Not Uploaded. Try Again');
             } else {
                 $output_array = array('response' => 'success', 'song' => array('song_id' => $song_id, 'id' => $uid, 'title' => $title, 'artist' => $artist, 'scimg' => $scimg, 'file' => htmlspecialchars($artist . ' - ' . $title, ENT_QUOTES), 'size' => '5', 'date' => date('Y-m-d H:i:s'), 'source' => 'soundcloud'));
             }
             //if song uploaded
         }
         //if song isn't streamable
     }
     // if song exists
     $this->output->set_output(json_encode($output_array));
 }