public static function importYtb($ytbLink, $folder, &$errorMsg = '') { $ytbCode = str_replace("&feature=related", "", PhocaGalleryYoutube::getCode(strip_tags($ytbLink))); $ytb = array(); $ytb['title'] = ''; $ytb['desc'] = ''; $ytb['filename'] = ''; $ytb['link'] = strip_tags($ytbLink); if (!function_exists("curl_init")) { $errorMsg = JText::_('COM_PHOCAGALLERY_YTB_NOT_LOADED_CURL'); return false; } else { if ($ytbCode == '') { $errorMsg = JText::_('COM_PHOCAGALLERY_YTB_URL_NOT_CORRECT'); return false; } else { // Data $cUrl = curl_init("http://gdata.youtube.com/feeds/api/videos/" . strip_tags($ytbCode)); curl_setopt($cUrl, CURLOPT_RETURNTRANSFER, 1); $xml = curl_exec($cUrl); curl_close($cUrl); $xml = str_replace('<media:', '<phcmedia', $xml); $xml = str_replace('</media:', '</phcmedia', $xml); $data = JFactory::getXML($xml, false); //Title if (isset($data->title)) { $ytb['title'] = (string) $data->title; } if ($ytb['title'] == '' && isset($data->phcmediagroup->phcmediatitle)) { $ytb['title'] = (string) $data->phcmediagroup->phcmediatitle; } if (isset($data->phcmediagroup->phcmediadescription)) { $ytb['desc'] = (string) $data->phcmediagroup->phcmediadescription; } // Thumbnail if (isset($data->phcmediagroup->phcmediathumbnail[0]['url'])) { $cUrl = curl_init(strip_tags((string) $data->phcmediagroup->phcmediathumbnail[0]['url'])); curl_setopt($cUrl, CURLOPT_RETURNTRANSFER, 1); $img = curl_exec($cUrl); curl_close($cUrl); } if ($img != '') { $cUrl = curl_init("http://img.youtube.com/vi/" . strip_tags($ytbCode) . "/0.jpg"); curl_setopt($cUrl, CURLOPT_RETURNTRANSFER, 1); $img = curl_exec($cUrl); curl_close($cUrl); } $ytb['filename'] = $folder . strip_tags($ytbCode) . '.jpg'; if (JFile::exists(JPATH_ROOT . DS . 'images' . DS . 'phocagallery' . DS . $ytb['filename'], $img)) { $errorMsg = JText::_('COM_PHOCAGALLERY_YTB_ERROR_VIDEO_EXISTS'); return false; } if (!JFile::write(JPATH_ROOT . DS . 'images' . DS . 'phocagallery' . DS . $ytb['filename'], $img)) { $errorMsg = JText::_('COM_PHOCAGALLERY_YTB_ERROR_WRITE_IMAGE'); return false; } } } return $ytb; }
public static function importYtb($ytbLink, $folder, &$errorMsg = '') { $ytbCode = str_replace("&feature=related", "", PhocaGalleryYoutube::getCode(strip_tags($ytbLink))); $ytb = array(); $ytb['title'] = ''; $ytb['desc'] = ''; $ytb['filename'] = ''; $ytb['link'] = strip_tags($ytbLink); if (!function_exists("curl_init")) { $errorMsg = JText::_('COM_PHOCAGALLERY_YTB_NOT_LOADED_CURL'); return false; } else { if ($ytbCode == '') { $errorMsg = JText::_('COM_PHOCAGALLERY_YTB_URL_NOT_CORRECT'); return false; } else { $paramsC = JComponentHelper::getParams('com_phocagallery'); $key = $paramsC->get('youtube_api_key', ''); $ssl = $paramsC->get('youtube_api_ssl', 0); // Data //$cUrl = curl_init("http://gdata.youtube.com/feeds/api/videos/".strip_tags($ytbCode)); $cUrl = curl_init('https://www.googleapis.com/youtube/v3/videos?id=' . strip_tags($ytbCode) . '&part=snippet&key=' . strip_tags($key)); if ($ssl == 0) { curl_setopt($cUrl, CURLOPT_SSL_VERIFYPEER, false); } else { curl_setopt($cUrl, CURLOPT_SSL_VERIFYPEER, true); } curl_setopt($cUrl, CURLOPT_RETURNTRANSFER, 1); $json = curl_exec($cUrl); curl_close($cUrl); $o = json_decode($json); if (!empty($o) && isset($o->error->message)) { $errorMsg = JText::_('COM_PHOCAGALLERY_YTB_ERROR_IMPORTING_DATA') . '(' . strip_tags($o->error->message) . ')'; return false; } else { if (!empty($o) && isset($o->items[0]->snippet)) { $oS = $o->items[0]->snippet; if (isset($oS->title)) { $ytb['title'] = (string) $oS->title; } if ($ytb['title'] == '' && isset($oS->localized->title)) { $ytb['title'] = (string) $oS->localized->title; } if (isset($oS->description)) { $ytb['desc'] = (string) $oS->description; } if ($ytb['desc'] == '' && isset($oS->localized->description)) { $ytb['desc'] = (string) $oS->localized->description; } if (isset($oS->thumbnails->standard->url)) { $cUrl = curl_init(strip_tags((string) $oS->thumbnails->standard->url)); if ($ssl == 0) { curl_setopt($cUrl, CURLOPT_SSL_VERIFYPEER, false); } else { curl_setopt($cUrl, CURLOPT_SSL_VERIFYPEER, true); } curl_setopt($cUrl, CURLOPT_RETURNTRANSFER, 1); $img = curl_exec($cUrl); curl_close($cUrl); } $ytb['filename'] = $folder . strip_tags($ytbCode) . '.jpg'; if ($img != '') { if (JFile::exists(JPATH_ROOT . DS . 'images' . DS . 'phocagallery' . DS . $ytb['filename'], $img)) { //$errorMsg = JText::_('COM_PHOCAGALLERY_YTB_ERROR_VIDEO_EXISTS'); //return false; //Overwrite the images } if (!JFile::write(JPATH_ROOT . DS . 'images' . DS . 'phocagallery' . DS . $ytb['filename'], $img)) { $errorMsg = JText::_('COM_PHOCAGALLERY_YTB_ERROR_WRITE_IMAGE'); return false; } } } else { $errorMsg = JText::_('COM_PHOCAGALLERY_YTB_ERROR_IMPORTING_DATA'); return false; } } // API 2 /*$xml = str_replace('<media:', '<phcmedia', $xml); $xml = str_replace('</media:', '</phcmedia', $xml); $data = JFactory::getXML($xml, false); //Title if (isset($data->title)) { $ytb['title'] = (string)$data->title; } if ($ytb['title'] == '' && isset($data->phcmediagroup->phcmediatitle)) { $ytb['title'] = (string)$data->phcmediagroup->phcmediatitle; } if (isset($data->phcmediagroup->phcmediadescription)) { $ytb['desc'] = (string)$data->phcmediagroup->phcmediadescription; } // Thumbnail if (isset($data->phcmediagroup->phcmediathumbnail[0]['url'])) { $cUrl = curl_init(strip_tags((string)$data->phcmediagroup->phcmediathumbnail[0]['url'])); curl_setopt($cUrl,CURLOPT_RETURNTRANSFER,1); $img = curl_exec($cUrl); curl_close($cUrl); } if ($img != '') { $cUrl = curl_init("http://img.youtube.com/vi/".strip_tags($ytbCode)."/0.jpg"); curl_setopt($cUrl,CURLOPT_RETURNTRANSFER,1); $img = curl_exec($cUrl); curl_close($cUrl); } $ytb['filename'] = $folder.strip_tags($ytbCode).'.jpg'; if (JFile::exists(JPATH_ROOT . DS . 'images' . DS . 'phocagallery' . DS . $ytb['filename'], $img)) { $errorMsg = JText::_('COM_PHOCAGALLERY_YTB_ERROR_VIDEO_EXISTS'); return false; } if (!JFile::write(JPATH_ROOT . DS . 'images' . DS . 'phocagallery' . DS . $ytb['filename'], $img)) { $errorMsg = JText::_('COM_PHOCAGALLERY_YTB_ERROR_WRITE_IMAGE'); return false; }*/ } } return $ytb; }
function import() { JRequest::checkToken() or die('Invalid Token'); $app = JFactory::getApplication(); //$post = JRequest::get('post'); $ytb_link = JRequest::getVar('ytb_link', '', 'post', 'string', JREQUEST_NOTRIM); $field = JRequest::getVar('field', '', 'post', 'string', JREQUEST_NOTRIM); $catid = JRequest::getVar('catid', 0, 'post', 'int'); //$ytb_code = PhocaGalleryYoutube::getCode(strip_tags($ytb_link)); $ytb_code = str_replace("&feature=related", "", PhocaGalleryYoutube::getCode(strip_tags($ytb_link))); $msg = $errorMsg = ''; $ytb = array(); $ytb['title'] = ''; $ytb['desc'] = ''; $ytb['filename'] = ''; $ytb['link'] = strip_tags($ytb_link); if (!function_exists("curl_init")) { $errorMsg .= JText::_('COM_PHOCAGALLERY_YTB_NOT_LOADED_CURL'); } else { if ($ytb_code == '') { $errorMsg .= JText::_('COM_PHOCAGALLERY_YTB_URL_NOT_CORRECT'); } else { $folder = ''; if ((int) $catid > 0) { $db =& JFactory::getDBO(); $query = 'SELECT c.userfolder' . ' FROM #__phocagallery_categories AS c' . ' WHERE c.id = ' . $db->Quote((int) $catid); $db->setQuery($query, 0, 1); $folderObj = $db->loadObject(); if (!$db->query()) { $this->setError($db->getErrorMsg()); return false; } if (isset($folderObj->userfolder) && $folderObj->userfolder != '') { $folder = $folderObj->userfolder . DS; // Save to category folder } else { $folder = ''; // No category folder - save to root } } else { $errorMsg .= JText::_('COM_PHOCAGALLERY_YTB_ERROR_NO_CATEGORY'); } // Data $cUrl = curl_init("http://gdata.youtube.com/feeds/api/videos/" . strip_tags($ytb_code)); curl_setopt($cUrl, CURLOPT_RETURNTRANSFER, 1); $xml = curl_exec($cUrl); curl_close($cUrl); $xml = str_replace('<media:', '<phcmedia', $xml); $xml = str_replace('</media:', '</phcmedia', $xml); $data = JFactory::getXML($xml, false); //Title if (isset($data->title)) { $ytb['title'] = (string) $data->title; } if ($ytb['title'] == '' && isset($data->phcmediagroup->phcmediatitle)) { $ytb['title'] = (string) $data->phcmediagroup->phcmediatitle; } if (isset($data->phcmediagroup->phcmediadescription)) { $ytb['desc'] = (string) $data->phcmediagroup->phcmediadescription; } // Thumbnail if (isset($data->phcmediagroup->phcmediathumbnail[0]['url'])) { $cUrl = curl_init(strip_tags((string) $data->phcmediagroup->phcmediathumbnail[0]['url'])); curl_setopt($cUrl, CURLOPT_RETURNTRANSFER, 1); $img = curl_exec($cUrl); curl_close($cUrl); } if ($img != '') { $cUrl = curl_init("http://img.youtube.com/vi/" . strip_tags($ytb_code) . "/0.jpg"); curl_setopt($cUrl, CURLOPT_RETURNTRANSFER, 1); $img = curl_exec($cUrl); curl_close($cUrl); } $ytb['filename'] = $folder . strip_tags($ytb_code) . '.jpg'; if (!JFile::write(JPATH_ROOT . DS . 'images' . DS . 'phocagallery' . DS . $ytb['filename'], $img)) { $errorMsg .= JText::_('COM_PHOCAGALLERY_YTB_ERROR_WRITE_IMAGE'); } } } JRequest::setVar('ytb_title', $ytb['title']); JRequest::setVar('ytb_desc', $ytb['desc']); JRequest::setVar('ytb_filename', $ytb['filename']); JRequest::setVar('ytb_link', $ytb['link']); if ($errorMsg != '') { $msg = $errorMsg; $import = ''; } else { $msg = JText::_('COM_PHOCAGALLERY_YTB_SUCCESS_IMPORT'); $import = '&import=1'; $app->getUserStateFromRequest($this->context . '.ytb_title', 'ytb_title', $ytb['title'], 'string'); $app->getUserStateFromRequest($this->context . '.ytb_desc', 'ytb_desc', $ytb['desc'], 'string'); $app->getUserStateFromRequest($this->context . '.ytb_filename', 'ytb_filename', $ytb['filename'], 'string'); $app->getUserStateFromRequest($this->context . '.ytb_link', 'ytb_link', $ytb['link'], 'string'); } $redirect = 'index.php?option=com_phocagallery&view=phocagalleryytb&tmpl=component&field=' . $field . '&catid=' . (int) $catid . $import; $this->setRedirect($redirect, $msg); }