PrsoAdvBrightCoveFtp

General App Functions.

Defined (1)

The class is defined in the following location(s).

/inc/VideoUploader/includes/inc_brightcove_ftp.php  
  1. class PrsoAdvBrightCoveFtp extends PrsoAdvVideoUploader { 
  2.  
  3. private $ftp_server = NULL; 
  4. private $username = NULL; 
  5. private $password = NULL; 
  6. private $publisher_id = NULL; 
  7. private $preparer = NULL; 
  8. private $manifext_filename = 'myManifest'; 
  9. private $ftp_conn_id = ''; 
  10. private $callback_url = ''; 
  11.  
  12. public function init_api( $validated_attachments ) { 
  13.  
  14. //Init api and Process video uploads 
  15. return $this->init_brightcove_ftp( $validated_attachments );; 
  16.  
  17.  
  18. public function get_video_url( $video_id = NULL ) { 
  19.  
  20. //Init vars 
  21. $bright_cove_url = 'http://www.brightcove.com'; 
  22.  
  23. return $bright_cove_url; 
  24.  
  25.  
  26.  
  27. private function init_brightcove_ftp( $validated_attachments ) { 
  28.  
  29. //Init vars 
  30. $manifest_path = NULL; 
  31. $ftp_conn_id = NULL; 
  32. $upload_results = array(); 
  33. $upload_manifest = FALSE; 
  34. $plugin_options = array(); 
  35.  
  36. //First try and get the plugin options 
  37. if( isset($this->plugin_options_slug) ) { 
  38. $plugin_options = get_option( $this->plugin_options_slug ); 
  39.  
  40. $this->plugin_error_log( $plugin_options ); 
  41.  
  42. if( $plugin_options !== FALSE && isset($plugin_options['bc_server'], $plugin_options['bc_username'], $plugin_options['bc_password'], $plugin_options['bc_publisher_id'], $plugin_options['bc_preparer']) ) { 
  43.  
  44. //Cache options values 
  45. $this->ftp_server = esc_attr( $plugin_options['bc_server'] ); 
  46. $this->username = esc_attr( $plugin_options['bc_username'] ); 
  47. $this->password = esc_attr( $plugin_options['bc_password'] ); 
  48. $this->publisher_id = esc_attr( $plugin_options['bc_publisher_id'] ); 
  49. $this->preparer = esc_attr( $plugin_options['bc_preparer'] ); 
  50.  
  51. //Init process 
  52. if( !empty($validated_attachments) && is_array($validated_attachments) ) { 
  53.  
  54. //Write xml manifest file for all validate video files 
  55. $manifest_path = $this->generate_manifest_xml( $validated_attachments ); 
  56.  
  57. //Init ftp connection 
  58. if( $manifest_path !== FALSE && file_exists($manifest_path) ) { 
  59.  
  60. $ftp_conn_id = $this->init_ftp_connection(); 
  61.  
  62. //Error check connection 
  63. if( $ftp_conn_id !== FALSE ) { 
  64.  
  65. //cache connection id 
  66. $this->ftp_conn_id = $ftp_conn_id; 
  67.  
  68. //Upload video files 
  69. $upload_results = $this->upload_video_files( $validated_attachments ); 
  70.  
  71. //Upload manifest xml file 
  72. if( !empty($upload_results) && is_array($upload_results) ) { 
  73. $upload_manifest = $this->upload_manifest_file( $manifest_path ); 
  74.  
  75. //Final error check, make sure manifest uploaded ok 
  76. if( $upload_manifest === FALSE ) { 
  77.  
  78. //Empty upload results array to ensure a redundant copy of 
  79. //video files remain in wp media library as backup 
  80. $upload_results = array(); 
  81.  
  82.  
  83.  
  84.  
  85.  
  86. } else { 
  87. $this->plugin_error_log( 'Brightcove FTP:: Missing options for this api.' ); 
  88.  
  89.  
  90. //Return the array of succefully uploaded files to be updated in gforms 
  91. //meta table and deleted from wp media library 
  92. return $upload_results; 
  93.  
  94. /** 
  95. * generate_manifest_xml 
  96. *  
  97. * Loops over all validate video files and creates 
  98. * the mainfest xml file 
  99. * Video Array Format: 
  100. * Array 
  101. [1] => Array //Each gravity forms upload field 
  102. [0] => Array //Each video file uploaded via this field 
  103. [wp_attachement_id] => 906 
  104. [mime_type] => video/quicktime 
  105. [file_path] => /Users/ben/Documents/Development/vhost/www.pressoholics.dev/wp-content/uploads/2013/03/cdeecad1844aca9e7b95fd66bc556df6.mov 
  106. [title] => Uploaded File: Form 6, Entry 4, File 0 
  107. [description] => This video was uploaded from your website, here are the details: Form 6, Entry 4, File 0 
  108.  
  109.  
  110. *  
  111. * @access private 
  112. * @author Ben Moody 
  113. */ 
  114. private function generate_manifest_xml( $validated_attachments ) { 
  115.  
  116. //Init vars 
  117. $result = FALSE; 
  118. $save_path = NULL; 
  119. $wp_wpload_dir_info = array(); 
  120. $doc = NULL; 
  121. $type = 'VIDEO_FULL'; 
  122. $encode_to = 'MP4'; 
  123. $encode_multiple = 'true'; 
  124. $conf_email = NULL; 
  125.  
  126. //Cache local save path for manifest 
  127. $wp_wpload_dir_info = wp_upload_dir(); 
  128.  
  129. if( isset($wp_wpload_dir_info['basedir'], $this->manifext_filename) ) { 
  130. $save_path = $wp_wpload_dir_info['basedir'] . '/' . $this->manifext_filename . '.xml'; 
  131.  
  132. //Cache confirmation email address 
  133. $conf_email = get_option( 'prso_gforms_youtube_main_options', NULL ); 
  134. if( isset($conf_email['confirmation_email']) ) { 
  135. $conf_email = $conf_email['confirmation_email']; 
  136.  
  137. $this->plugin_error_log( 'Email: ' . $conf_email ); 
  138.  
  139. //Start xml write process 
  140. if( !empty($save_path) ) { 
  141.  
  142. $doc = new DomDocument( '1.0', 'UTF-8' ); 
  143.  
  144. //Create publisher-upload-manifest element 
  145. $pub_manifest = $doc->createElement( 'publisher-upload-manifest' ); 
  146. $pub_manifest_node = $doc->appendChild( $pub_manifest ); 
  147.  
  148. //Add publisher-upload-manifest attributes 
  149. $pub_manifest_node->setAttribute( 'publisher-id', $this->publisher_id ); 
  150. $pub_manifest_node->setAttribute( 'preparer', $this->preparer ); 
  151. $pub_manifest_node->setAttribute( 'report-success', 'true' ); 
  152.  
  153. //Create notify element 
  154. $notify = $doc->createElement( 'notify' ); 
  155. $notify_node = $pub_manifest->appendChild( $notify ); 
  156.  
  157. //Add callback attributes 
  158. $notify_node->setAttribute( 'email', $conf_email ); 
  159.  
  160.  
  161. //Create callback element 
  162. //$callback = $doc->createElement( 'callback' ); 
  163. //$callback_node = $pub_manifest->appendChild( $callback ); 
  164.  
  165. //Add callback attributes 
  166. //$callback_node->setAttribute( 'entity-url', $this->callback_url ); 
  167.  
  168.  
  169.  
  170. //Create video file elements 
  171. foreach( $validated_attachments as $field_id => $upload_fields ) { 
  172. foreach( $upload_fields as $key => $upload_data ) { 
  173.  
  174. //Init vars 
  175. $file_path_info = NULL; 
  176. $basename = NULL; 
  177. $filename = NULL; 
  178. $ref_id = NULL; 
  179.  
  180. if( isset($upload_data['file_path'], $upload_data['wp_attachement_id']) ) { 
  181.  
  182. //Get file path info 
  183. $file_path_info = pathinfo( $upload_data['file_path'] ); 
  184.  
  185. //Create and add Asset element for this file 
  186. if( isset($file_path_info['basename'], $file_path_info['filename']) ) { 
  187.  
  188. $basename = $file_path_info['basename']; 
  189.  
  190. $filename = $file_path_info['filename']; 
  191.  
  192. //Cache ref_id - format:: podders_{$wp_attachement_id} 
  193. $ref_id = 'podders_' . rand(0, 1000) . '_' . $upload_data['wp_attachement_id']; 
  194.  
  195. //Add Asset element to manifest for this file 
  196. $file_asset = $doc->createElement( 'asset' ); 
  197. $file_asset_node = $pub_manifest->appendChild( $file_asset ); 
  198.  
  199. //Add Asset attributes 
  200. $file_asset_node->setAttribute( 'filename', $basename );  
  201. $file_asset_node->setAttribute( 'refid', $ref_id ); 
  202. $file_asset_node->setAttribute( 'type', $type ); 
  203. $file_asset_node->setAttribute( 'encode-to', $encode_to ); 
  204. $file_asset_node->setAttribute( 'encode-multiple', $encode_multiple ); 
  205.  
  206.  
  207. //Create and add Title element for this file 
  208. if( isset($ref_id, $upload_data['title'], $upload_data['description']) ) { 
  209.  
  210. //Add Title element to manifest for this file 
  211. $file_title = $doc->createElement( 'title' ); 
  212. $file_title_node = $pub_manifest->appendChild( $file_title ); 
  213.  
  214. //Add Title attributes 
  215. $file_title_node->setAttribute( 'name', $upload_data['title'] ); 
  216. $file_title_node->setAttribute( 'refid', $ref_id ); 
  217. $file_title_node->setAttribute( 'video-full-refid', $ref_id ); 
  218.  
  219. //Create description element 
  220. $short_desc = $doc->createElement( 'short-description', $upload_data['description'] ); 
  221.  
  222. //Append description to Title element 
  223. $file_title->appendChild( $short_desc ); 
  224.  
  225.  
  226.  
  227.  
  228.  
  229. if( ($result = $doc->save( $save_path )) !== FALSE ) { 
  230. //Return full path to manifest xml file 
  231. $result = $save_path;  
  232. } else { 
  233. $this->plugin_error_log( 'Brightcove FTP:: Error saving Manifest xml file.' ); 
  234.  
  235.  
  236. return $result; 
  237.  
  238. private function init_ftp_connection() { 
  239.  
  240. //Init vars 
  241. $conn_id = FALSE; 
  242. $login_result = FALSE; 
  243.  
  244. if( isset($this->ftp_server, $this->username, $this->password) ) { 
  245.  
  246. //Connect to FTP server (port 21) 
  247. $conn_id = ftp_connect( $this->ftp_server, 21 ); 
  248.  
  249. //Check for error 
  250. if( $conn_id !== FALSE ) { 
  251.  
  252. //Send access params 
  253. $login_result = ftp_login( $conn_id, $this->username, $this->password ); 
  254.  
  255. //Detect result - allow return of conn_id if succesfull 
  256. if( $login_result === FALSE ) { 
  257. $conn_id = FALSE; 
  258. $this->plugin_error_log( 'Brightcove FTP:: Error in ftp_login.' ); 
  259.  
  260. } else { 
  261. $this->plugin_error_log( 'Brightcove FTP:: Error in ftp_connect.' ); 
  262.  
  263. } else { 
  264. $this->plugin_error_log( 'Brightcove FTP:: Missing FTP credentials.' ); 
  265.  
  266. return $conn_id; 
  267.  
  268. private function upload_video_files( $validated_attachments ) { 
  269.  
  270. //Init vars 
  271. $result = FALSE; 
  272.  
  273. //Loop validate video files array and upload each video found 
  274. foreach( $validated_attachments as $field_id => $upload_fields ) { 
  275. foreach( $upload_fields as $key => $upload_data ) { 
  276.  
  277. //Reset result just to be sure 
  278. $result = FALSE; 
  279.  
  280. //Try and upload file 
  281. if( isset($upload_data['file_path']) ) { 
  282.  
  283. $result = $this->upload_file_ftp( $upload_data['file_path'] ); 
  284.  
  285. //Error check, if there was an error remove video data from array 
  286. //This will ensure the file remains in wordpress as a backup 
  287. if( $result === FALSE ) { 
  288. unset( $validated_attachments[$field_id][$key] ); 
  289. $this->plugin_error_log( 'Upload Failed:: ' . $upload_data['file_path'] ); 
  290. } else { 
  291. $this->plugin_error_log( 'File Uploaded:: ' . $upload_data['file_path'] ); 
  292.  
  293.  
  294.  
  295. //Return array of files successfully uploaded 
  296. //These will be removed from the wordpress media library and server 
  297. return $validated_attachments; 
  298.  
  299. private function upload_manifest_file( $manifest_path ) { 
  300.  
  301. //Init vars 
  302. $result = FALSE; 
  303.  
  304. //Upload manifest file 
  305. $result = $this->upload_file_ftp( $manifest_path ); 
  306.  
  307. return $result; 
  308.  
  309. private function upload_file_ftp( $local_file_path ) { 
  310.  
  311. //Init vars 
  312. $remote_file = NULL; 
  313. $result = FALSE; 
  314.  
  315. //Check file exsits 
  316. if( file_exists($local_file_path) && isset($this->ftp_conn_id) ) { 
  317.  
  318. //First try and get remote_file value, this is the filename of upload 
  319. $remote_file = pathinfo( $local_file_path ); 
  320.  
  321. if( isset($remote_file['basename']) ) { 
  322. $remote_file = $remote_file['basename']; 
  323. } else { 
  324. $remote_file = NULL; 
  325.  
  326. if( !empty($remote_file) ) { 
  327.  
  328. //Turn on ftp passive mode 
  329. //ftp_pasv( $this->ftp_conn_id, TRUE ); 
  330.  
  331. //Perform file upload 
  332. $result = ftp_put( $this->ftp_conn_id, $remote_file, $local_file_path, FTP_ASCII ); 
  333.  
  334. } else { 
  335. $this->plugin_error_log( 'Brightcove FTP:: Error getting file basename.' ); 
  336.  
  337. } else { 
  338. $this->plugin_error_log( 'Brightcove FTP:: Can\'t find file to upload.' ); 
  339.  
  340. return $result; 
  341.