GDWPMBantuan

The Google Drive WP Media GDWPMBantuan class.

Defined (1)

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

/google-drive-wp-media.php  
  1. class GDWPMBantuan { 
  2.  
  3. protected $scope = array('https://www.googleapis.com/auth/drive'); 
  4.  
  5. private $_service; 
  6.  
  7. public function __construct( $clientId, $serviceAccountName, $key ) { 
  8. $client = new Google_Client(); 
  9. $client->setApplicationName("Google Drive WP Media"); 
  10. $client->setClientId( $clientId ); 
  11.  
  12. $client->setAssertionCredentials( new Google_AssertionCredentials( 
  13. $serviceAccountName,  
  14. $this->scope,  
  15. $this->getKonten( $key ) ) 
  16. ); 
  17. $this->_service = new Google_DriveService($client); 
  18.  
  19. public function __get( $name ) { 
  20. return $this->_service->$name; 
  21.  
  22. public function getKonten( $url ) { 
  23. if(function_exists('curl_version')) { 
  24. $data = curl_init(); 
  25. curl_setopt($data, CURLOPT_RETURNTRANSFER, 1); 
  26. curl_setopt($data, CURLOPT_URL, $url); 
  27. curl_setopt($data, CURLOPT_FOLLOWLOCATION, TRUE); 
  28. curl_setopt($data, CURLOPT_SSL_VERIFYPEER, FALSE);  
  29. curl_setopt($data, CURLOPT_SSL_VERIFYHOST, FALSE);  
  30. curl_setopt($data, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 6.1; rv:2.2) Gecko/20110201'); 
  31. $hasil = curl_exec($data); 
  32. curl_close($data); 
  33. return $hasil; 
  34. }else{ 
  35. $hasil = file_get_contents(str_replace(' ', '%20', $url)); 
  36. return $hasil; 
  37.  
  38. public function getAbout( ) { 
  39. return $this->_service->about->get(); 
  40.  
  41. public function buangFile( $fileId ) { 
  42. $result = $this->_service->files->delete($fileId); 
  43. if(empty($result)) { 
  44. return true; 
  45. }else{ 
  46. return false; 
  47.  
  48. public function getNameFromId( $fileId ) { 
  49. $file_proper = $this->_service->files->get($fileId); 
  50. $file_name = $file_proper->title; 
  51. return $file_name; 
  52.  
  53. public function insertProperty($fileId, $key, $value, $visibility = 'PUBLIC') { 
  54. if(!empty($value) || $value != '') {  
  55. $newProperty = new Google_Property(); 
  56. $newProperty->setKey($key); 
  57. $newProperty->setValue($value); 
  58. $newProperty->setVisibility($visibility); 
  59. return $this->_service->properties->insert($fileId, $newProperty); 
  60. }else{ 
  61. return false; 
  62.  
  63. public function createFileFromPath( $path, $fileName, $description, Google_ParentReference $fileParent = null ) { 
  64. $mimeType = wp_check_filetype($fileName); 
  65. $file = new Google_DriveFile(); 
  66. $file->setTitle( $fileName ); 
  67. $file->setDescription( $description ); 
  68. $file->setMimeType( $mimeType['type'] );  
  69. if( $fileParent ) { 
  70. $file->setParents( array( $fileParent ) ); 
  71. $gdwpm_opsi_chunk = get_option('gdwpm_opsi_chunk'); 
  72. $chunks = $gdwpm_opsi_chunk['drive']['chunk']; 
  73. $max_retries = (int) $gdwpm_opsi_chunk['drive']['retries']; 
  74. $chunkSize = (1024 * 1024) * (int) $chunks; // 2mb chunk 
  75. $fileupload = new Google_MediaFileUpload($mimeType['type'], null, true, $chunkSize); 
  76. $fileupload->setFileSize(filesize($path)); 
  77. $mkFile = $this->_service->files->insert($file, array('mediaUpload' => $fileupload)); 
  78. $status = false; 
  79. $handle = fopen($path, "rb"); 
  80. while (!$status && !feof($handle)) { 
  81. $max = false; 
  82. for ($i=1; $i<=$max_retries; $i++) { 
  83. $chunked = fread($handle, $chunkSize); 
  84. if ($chunked) { 
  85. $createdFile = $fileupload->nextChunk($mkFile, $chunked); 
  86. break; 
  87. }elseif($i == $max_retries) { 
  88. $max = true; 
  89. if($max) { 
  90. if($createdFile) { 
  91. $this->_service->files->trash( $createdFile['id'] ); 
  92. $createdFile = false;  
  93. break; 
  94. fclose($handle); 
  95. if($createdFile) { 
  96. return $createdFile['id']; 
  97. }else{ 
  98. return false; 
  99.  
  100. public function createFolder( $name ) { 
  101. $file = new Google_DriveFile(); 
  102. $file->setTitle( $name ); 
  103. $file->setMimeType( 'application/vnd.google-apps.folder' ); 
  104. $createdFolder = $this->_service->files->insert($file, array('mimeType' => 'application/vnd.google-apps.folder')); 
  105. return $createdFolder['id']; 
  106.  
  107. public function setPermissions( $fileId, $value, $role = 'writer', $type = 'user' ) { 
  108. $perm = new Google_Permission(); 
  109. $perm->setValue( $value ); 
  110. $perm->setType( $type ); 
  111. $perm->setRole( $role ); 
  112.  
  113. return $this->_service->permissions->insert($fileId, $perm); 
  114.  
  115. public function getFolderIdByName( $name ) { 
  116. $parameters = array('q' => "mimeType = 'application/vnd.google-apps.folder'", 'maxResults' => 50); 
  117. $files = $this->_service->files->listFiles($parameters); 
  118. foreach( $files['items'] as $item ) { 
  119. if( $item['title'] == $name ) { 
  120. return $item['id']; 
  121. break; 
  122. return false; 
  123.  
  124. public function getFilesInFolder($folderId, $maxResults, $pageToken = '', $in_type = 'radio') { 
  125. if($in_type == 'radio') { 
  126. $div_id = 'hasil'; 
  127. $id_max = 'maxres'; 
  128. $id_folid = 'folid'; 
  129. $id_tblpagi = 'paginasi'; 
  130. $div_hal = 'halaman'; 
  131. $div_pagi = 'vaginasi'; 
  132. $opsi_kecing = 'gdwpm_kecing_hal'; 
  133. $in_name = 'gdwpm_berkas_terpilih[]'; 
  134. }elseif($in_type == 'checkbox') { 
  135. $div_id = 'hasil_del'; 
  136. $id_max = 'maxres_del'; 
  137. $id_folid = 'folid_del'; 
  138. $id_tblpagi = 'paginasi_del'; 
  139. $div_hal = 'halaman_del'; 
  140. $div_pagi = 'vaginasi_del'; 
  141. $opsi_kecing = 'gdwpm_kecing_hal_del'; 
  142. $in_name = 'gdwpm_buang_berkas_terpilih[]'; 
  143. }else{ 
  144. $in_type = 'checkbox'; 
  145. $div_id = 'hasil_gal'; 
  146. $id_max = 'maxres_gal'; 
  147. $id_folid = 'folid_gal'; 
  148. $id_tblpagi = 'paginasi_gal'; 
  149. $div_hal = 'halaman_gal'; 
  150. $div_pagi = 'vaginasi_gal'; 
  151. $opsi_kecing = 'gdwpm_kecing_hal_gal'; 
  152. $in_name = 'gdwpm_berkas_gallery[]'; 
  153. //setup 1st pagetokn is always enpty n create pagintion butt 
  154. $haldepan = 1; 
  155. ////$hal = '<input type="hidden" id="maxres" value="'.$maxResults.'" /><button id="halaman" value="">'.$haldepan.'</button>'; 
  156. $parameters = array('maxResults' => $maxResults); 
  157. $pageTokenInput = $pageToken; 
  158. $gdwpm_kecing_hal = get_option($opsi_kecing); 
  159. if (empty($pageToken) || $pageToken == '') { 
  160. // generate halaman 
  161. //if($gdwpm_kecing_hal || !empty($gdwpm_kecing_hal)) { 
  162. //delete_option($opsi_kecing); 
  163. //} 
  164. $gdwpm_kecing_hal = array(); 
  165. $errormes = ''; 
  166. $halarr = array($haldepan => 'bantuanhalamansatu'); 
  167. do { 
  168. $haldepan++; 
  169. try { 
  170. if($haldepan == 1) {$pageToken = '';} //halman prtama pokoke token kudu kosong 
  171. $parameters['pageToken'] = $pageToken; 
  172. $children = $this->_service->children->listChildren($folderId, $parameters); 
  173. $pageToken = $children->getNextPageToken(); 
  174. if($pageToken) { 
  175. //$hal .= ' <button id="halaman" value="'.$pageToken.'">'.$haldepan.'</button>'; 
  176. $halarr[$haldepan] = $pageToken; 
  177. if($haldepan % 10 == 0) {sleep(1);} 
  178. //}elseif($haldepan > 1) { 
  179. //cek n buang halman trakir jika kosong 
  180. //$parameters['pageToken'] = $halarr[$haldepan - 1]; 
  181. //$files = $this->_service->children->listChildren($folderId, $parameters); 
  182. //$result = array(); 
  183. //if(count(array_merge($result, $files->getItems())) < 1) { 
  184. //unset($halarr[$haldepan - 1]); 
  185. //} 
  186. } catch (Exception $e) { 
  187. $errormes = "<kbd>An error occurred: " . $e->getMessage() . "</kbd>"; 
  188. $haldepan -= 1; 
  189. $pageToken = $halarr[$haldepan]; //NULL; 
  190. sleep(1); 
  191. } while ($pageToken); 
  192. unset($parameters['pageToken']); 
  193. $gdwpm_kecing_hal[$folderId] = $halarr; 
  194. update_option($opsi_kecing, $gdwpm_kecing_hal); 
  195. }else{ 
  196. $parameters['pageToken'] = $pageToken; 
  197. $daftarfile = ''; 
  198. if(count($halarr) <= 1 || $pageToken != '') { 
  199. if($pageToken == 'bantuanhalamansatu') { 
  200. unset($parameters['pageToken']); 
  201. $folder_proper = $this->_service->files->get($folderId); 
  202. $folder_name = $folder_proper->title; 
  203. $i = 0; 
  204. $daftarfile = '<div id="'.$div_id.'"><table id="box-table-a" summary="File Folder" class="'.$id_tblpagi.'"><thead><tr><th scope="col"><span class="ui-icon ui-icon-check"></span></th><th scope="col">File ID</th><th scope="col">Title</th><!--<th scope="col">Description</th>--><th scope="col">Size</th><th scope="col">Action</th></tr></thead>'; 
  205. $children = $this->_service->children->listChildren($folderId, $parameters); 
  206. foreach ($children->getItems() as $child) { 
  207. $i++; if($i == 1 && $in_type == 'radio') {$checked = 'checked';}else{$checked = '';} 
  208. if($maxResults != $i && $maxResults > 30 && $i % 20 == 0) {sleep(1);} 
  209. $fileId = $child->getId();  
  210. $file = $this->_service->files->get($fileId); //getDescription getMimeType 
  211. $file_mime = $file->getMimeType(); 
  212. $file_title = $file->getTitle(); 
  213. $file_desc = $file->getDescription(); 
  214. $file_icon = $file->getIconLink(); 
  215. $file_md5 = $file->getMd5Checksum(); 
  216. $file_size = size_format($file->getFileSize(), 2); 
  217. $file_thumb = $file->getThumbnailLink(); // str_replace('=s220', '=s300', $file->getThumbnailLink());  
  218. $view = '<a href="https://docs.google.com/uc?id='.$fileId.'&export=download" title="Open link in a new window" target="_blank" class="tabeksen">Download</a>'; 
  219. $file_pptis = ''; 
  220. if(strpos($file_mime, 'image') !== false) { 
  221. $view = '<a href="'. GD_BASE_PATH .$fileId.'" title="Open link in a new window" target="_blank" class="tabeksen">View</a>'; 
  222. $properties = $this->_service->properties->listProperties($fileId); 
  223. $getfile_pptis = $properties->getItems(); 
  224. if(count($getfile_pptis) > 0) { 
  225. $file_pptis = $getfile_pptis[0]->getValue(); 
  226. // selfWidth:xx selfHeight:xx thumbId:xxx thumbWidth:xx thumbHeight:xx 
  227. preg_match_all('/(\w+):("[^"]+"|\S+)/', $file_pptis, $matches); 
  228. $img_meta = array_combine($matches[1], $matches[2]); 
  229. if(array_key_exists('thumbId', $img_meta)) { 
  230. $file_thumb = GD_BASE_PATH . $img_meta['thumbId']; 
  231. }  
  232. $valson = json_encode(array($file_mime, $file_title, $fileId, $file_desc, $folder_name, $file_pptis)); 
  233. $daftarfile .= '<tbody><tr><td><input type="'.$in_type.'" name="'.$in_name.'" value="'.base64_encode($valson).'" ' . $checked . '></td><td class="kolom_file" title="' . $file_thumb . '">'.$fileId.'</td>'; 
  234. $daftarfile .= '<td title="' . $file_desc . '"><img src="' . $file_icon . '" title="' . $file_mime . '"> ' . $file_title . '</td>'; 
  235. $daftarfile .= '<!--<td>' . $file_desc . '</td>-->'; 
  236. $daftarfile .= '<td title="md5Checksum : ' . $file_md5 . '">' . $file_size . '</td>'; 
  237. $daftarfile .= '<td>' . $view . ' | <a href="https://docs.google.com/file/d/'.$fileId.'/preview?TB_iframe=true&width=600&height=550" title="'.$file_title.' ('.$fileId.')" class="thickbox tabeksen">Preview</a></td></tr>'; 
  238. $daftarfile .= '</tbody></table></div><br/>'; 
  239.  
  240.  
  241. // merangkai paginasi soretempe 
  242. $range = 5;  
  243. $showitems = ($range * 2)+1;  
  244. $hal_folderid = $gdwpm_kecing_hal[$folderId]; 
  245. $halterlihat = array_search($pageToken, $hal_folderid); 
  246. if(empty($halterlihat)) {$halterlihat = 1;} 
  247. $totalhal = count($hal_folderid); 
  248. if(1 != $totalhal) 
  249. $halsiap = '<input type="hidden" id="'.$id_max.'" value="'.$maxResults.'" /><input type="hidden" id="'.$id_folid.'" value="'.$folderId.'" />'; 
  250. if($halterlihat > 2 && $halterlihat > $range+1 && $showitems < $totalhal) $halsiap .= '<button id="'.$div_hal.'" value="'.$hal_folderid[1].'">«</button>'; 
  251. if($halterlihat > 1 && $showitems < $totalhal) $halsiap .= '<button id="'.$div_hal.'" value="'.$hal_folderid[$halterlihat - 1].'">‹</button>'; 
  252.  
  253. for ($j=1; $j <= $totalhal; $j++) 
  254. if (1 != $totalhal &&( !($j >= $halterlihat+$range+1 || $j <= $halterlihat-$range-1) || $totalhal <= $showitems )) 
  255. if($halterlihat == $j && $pageTokenInput != '') {$disable_butt = ' disabled';}else{$disable_butt = '';} 
  256. $halsiap .= '<button id="'.$div_hal.'" value="'.$hal_folderid[$j].'"'.$disable_butt.'>'.$j.'</button>'; 
  257.  
  258. if ($halterlihat < $totalhal && $showitems < $totalhal) $halsiap .= '<button id="'.$div_hal.'" value="'.$hal_folderid[$halterlihat + 1].'">›</button>'; 
  259. if ($halterlihat < $totalhal-1 && $halterlihat+$range-1 < $totalhal && $showitems < $totalhal) $halsiap .= '<button id="'.$div_hal.'" value="'.$hal_folderid[$totalhal].'">»</button>'; 
  260.  
  261. $vaginasi = '<div id="'.$div_pagi.'">'.$halsiap.'</div>'; 
  262. $daftarfile .= $vaginasi; 
  263. if($i == 0 && $totalhal > 1 && $halterlihat == $totalhal) {$daftarfile = $vaginasi;} 
  264. return array($daftarfile, $i, $totalhal, $halterlihat);//, $halterlihat, $totalhal);//items, items onpage, currentpage, totalpage 
  265.