src/AppBundle/Controller/Admin/EduCloudController.php line 693

Open in your IDE?
  1. <?php
  2. namespace AppBundle\Controller\Admin;
  3. use AppBundle\Common\ArrayToolkit;
  4. use Biz\Sms\Service\SmsService;
  5. use AppBundle\Common\FileToolkit;
  6. use Biz\CloudPlatform\Client\AbstractCloudAPI;
  7. use Biz\CloudPlatform\CloudAPIFactory;
  8. use Biz\CloudPlatform\IMAPIFactory;
  9. use Biz\CloudPlatform\KeyApplier;
  10. use Biz\CloudPlatform\Service\AppService;
  11. use Biz\Lms\Service\LmsService;
  12. use Biz\Role\Util\PermissionBuilder;
  13. use Biz\Sms\Constant\SmsConstants;
  14. use Biz\Util\EdusohoLiveClient;
  15. use Imagine\Gd\Imagine;
  16. use Imagine\Image\Box;
  17. use Symfony\Component\HttpFoundation\Request;
  18. use Symfony\Component\HttpFoundation\Response;
  19. use Symfony\Component\Security\Core\Encoder\MessageDigestPasswordEncoder;
  20. class EduCloudController extends BaseController
  21. {
  22.     //概览页,产品简介页
  23.     public function myCloudAction(Request $request)
  24.     {
  25.         // @apitodo 需改成leaf
  26.         try {
  27.             $api CloudAPIFactory::create('root');
  28.             $info $api->get('/me');
  29.         } catch (\RuntimeException $e) {
  30.             return $this->render('admin/edu-cloud/cloud-error.html.twig', []);
  31.         }
  32.         $this->refreshCloudSchoolSetting($info);
  33.         if (isset($info['accessCloud']) && != $info['accessCloud']) {
  34.             return $this->redirect($this->generateUrl('admin_my_cloud_overview'));
  35.         }
  36.         if (!isset($info['accessCloud']) || $this->getWebExtension()->isTrial() || == $info['accessCloud']) {
  37.             $trialHtml $this->getCloudCenterExperiencePage();
  38.             return $this->render('admin/edu-cloud/cloud.html.twig', [
  39.                 'content' => $trialHtml['content'],
  40.             ]);
  41.         }
  42.         $unTrial file_get_contents('http://open.edusoho.com/api/v1/block/cloud_guide');
  43.         $unTrialHtml json_decode($unTrialtrue);
  44.         return $this->render('admin/edu-cloud/cloud.html.twig', [
  45.             'content' => $unTrialHtml['content'],
  46.         ]);
  47.     }
  48.     //概览页,服务概况页
  49.     // refactor
  50.     public function myCloudOverviewAction(Request $request)
  51.     {
  52.         try {
  53.             $api CloudAPIFactory::create('root');
  54.             $isBinded $this->getAppService()->getBinded();
  55.             $overview $api->get("/cloud/{$api->getAccessKey()}/overview");
  56.             $schoolInfo $api->get('/me');
  57.         } catch (\RuntimeException $e) {
  58.             return $this->render('admin/edu-cloud/cloud-error.html.twig', []);
  59.         }
  60.         $this->refreshCloudSchoolSetting($schoolInfo);
  61.         if (isset($overview['enabled'])) {
  62.             $paidService = [];
  63.             $unPaidService = [];
  64.             $this->getSettingService()->set('cloud_status', [
  65.                 'enabled' => $overview['enabled'],
  66.                 'locked' => $overview['locked'],
  67.                 'accessCloud' => $overview['accessCloud'],
  68.             ]);
  69.         }
  70.         return $this->render('admin/edu-cloud/overview/index.html.twig', [
  71.             'isBinded' => $isBinded,
  72.             'overview' => $overview,
  73.         ]);
  74.     }
  75.     public function attachmentAction(Request $request)
  76.     {
  77.         $attachment $this->getSettingService()->get('cloud_attachment', []);
  78.         $defaultData = ['article' => 0'course' => 0'classroom' => 0'group' => 0'question' => 0];
  79.         $default array_merge($defaultData, ['enable' => 0'fileSize' => 500]);
  80.         $attachment array_merge($default$attachment);
  81.         if ('POST' == $request->getMethod()) {
  82.             $attachment $request->request->all();
  83.             $attachment array_merge($default$attachment);
  84.             $this->getSettingService()->set('cloud_attachment'$attachment);
  85.             $this->setFlashMessage('success''site.save.success');
  86.         }
  87.         //云端视频判断
  88.         try {
  89.             $api CloudAPIFactory::create('root');
  90.             $info $api->get('/me');
  91.         } catch (\RuntimeException $e) {
  92.             return $this->render('admin/edu-cloud/video-error.html.twig', []);
  93.         }
  94.         return $this->render('admin/edu-cloud/cloud-attachment.html.twig', [
  95.             'attachment' => $attachment,
  96.             'info' => $info,
  97.         ]);
  98.     }
  99.     //云视频概览页
  100.     public function videoOverviewAction(Request $request)
  101.     {
  102.         if ($this->getWebExtension()->isTrial()) {
  103.             return $this->render('admin/edu-cloud/video/trial.html.twig', []);
  104.         }
  105.         if (!($this->isVisibleCloud())) {
  106.             return $this->redirect($this->generateUrl('admin_my_cloud_overview'));
  107.         }
  108.         $storageSetting $this->getSettingService()->get('storage', []);
  109.         //云端视频判断
  110.         try {
  111.             $api CloudAPIFactory::create('root');
  112.             $overview $api->get('/me/storage/overview');
  113.         } catch (\RuntimeException $e) {
  114.             return $this->render('admin/edu-cloud/video-error.html.twig', []);
  115.         }
  116.         if ((isset($storageSetting['upload_mode']) && 'local' == $storageSetting['upload_mode']) || !isset($storageSetting['upload_mode'])) {
  117.             return $this->render('admin/edu-cloud/video/without-enable.html.twig');
  118.         }
  119.         $overview['video']['isBuy'] = isset($overview['video']['isBuy']) ? false true;
  120.         $overview['yearPackage']['isBuy'] = isset($overview['yearPackage']['isBuy']) ? false true;
  121.         $spaceItems $this->dealItems($overview['video']['spaceItems']);
  122.         $flowItems $this->dealItems($overview['video']['flowItems']);
  123.         return $this->render('admin/edu-cloud/video/overview.html.twig', [
  124.             'video' => $overview['video'],
  125.             'space' => isset($overview['space']) ? $overview['space'] : null,
  126.             'flow' => isset($overview['flow']) ? $overview['flow'] : null,
  127.             'yearPackage' => $overview['yearPackage'],
  128.             'spaceItems' => $spaceItems,
  129.             'flowItems' => $flowItems,
  130.         ]);
  131.     }
  132.     public function aiOverviewAction(Request $request)
  133.     {
  134.         $routingName 'service_ai_menu';
  135.         $params = ['item' => 'overview'];
  136.         $url $this->getAppService()->getTokenLoginUrl($routingName$params);
  137.         return $this->redirect($url);
  138.     }
  139.     public function videoSwitchAction(Request $request)
  140.     {
  141.         if ('POST' == $request->getMethod()) {
  142.             $set $request->request->all();
  143.             $storageSetting $this->getSettingService()->get('storage', []);
  144.             $storageSetting array_merge($storageSetting$set);
  145.             $this->getSettingService()->set('storage'$storageSetting);
  146.             return $this->redirect($this->generateUrl('admin_cloud_video_overview'));
  147.         }
  148.     }
  149.     private function dealItems($data)
  150.     {
  151.         if (empty($data)) {
  152.             for ($i 7$i 0; --$i) {
  153.                 $items['date'][] = date('Y-m-d'strtotime('-'.$i.'days'));
  154.                 $items['amount'][] = 0;
  155.             }
  156.             return $items;
  157.         }
  158.         foreach ($data as $value) {
  159.             $items['date'][] = $value['date'];
  160.             $items['amount'][] = $value['amount'];
  161.         }
  162.         return $items;
  163.     }
  164.     public function videoSettingAction(Request $request)
  165.     {
  166.         if ($this->getWebExtension()->isTrial()) {
  167.             return $this->render('admin/edu-cloud/video/trial.html.twig', []);
  168.         }
  169.         if (!($this->isVisibleCloud())) {
  170.             return $this->redirect($this->generateUrl('admin_my_cloud_overview'));
  171.         }
  172.         $storageSetting $this->getSettingService()->get('storage', []);
  173.         if ((isset($storageSetting['upload_mode']) && 'local' == $storageSetting['upload_mode']) || !isset($storageSetting['upload_mode'])) {
  174.             return $this->redirect($this->generateUrl('admin_cloud_video_overview'));
  175.         }
  176.         $storageSetting $this->getSettingService()->get('storage', []);
  177.         $default = [
  178.             'upload_mode' => 'local',
  179.             'video_h5_enable' => 1,
  180.             'enable_playback_rates' => 0,
  181.             'video_quality' => 'high',
  182.             'video_audio_quality' => 'high',
  183.             'video_watermark' => 0,
  184.             'video_watermark_image' => '',
  185.             'video_embed_watermark_image' => '',
  186.             'video_watermark_position' => 'topright',
  187.             'video_fingerprint' => 0,
  188.             'video_fingerprint_time' => 0.5,
  189.             'video_header' => null,
  190.             'video_auto_play' => 'true',
  191.         ];
  192.         if ('POST' == $request->getMethod()) {
  193.             $set $request->request->all();
  194.             $storageSetting array_merge($default$storageSetting$set);
  195.             $this->getSettingService()->set('storage'$storageSetting);
  196.             $this->setFlashMessage('success''site.save.success');
  197.             return $this->createJsonResponse(true);
  198.         } else {
  199.             $storageSetting array_merge($default$storageSetting);
  200.             $this->getSettingService()->set('storage'$storageSetting);
  201.         }
  202.         try {
  203.             $api CloudAPIFactory::create('root');
  204.             $overview $api->get('/me/storage/overview');
  205.         } catch (\RuntimeException $e) {
  206.             return $this->render('admin/edu-cloud/video-error.html.twig', []);
  207.         }
  208.         try {
  209.             $headLeader $this->getUploadFileService()->getFileByTargetType('headLeader');
  210.         } catch (\RuntimeException $e) {
  211.             $headLeader null;
  212.         }
  213.         return $this->render('admin/edu-cloud/video/setting.html.twig', [
  214.             'storageSetting' => $storageSetting,
  215.             'headLeader' => $headLeader,
  216.             'video' => $overview['video'],
  217.         ]);
  218.     }
  219.     public function videoControlAction(Request $request)
  220.     {
  221.         if ('POST' == $request->getMethod()) {
  222.             $set $request->request->all();
  223.             $storageSetting $this->getSettingService()->get('storage', []);
  224.             $storageSetting array_merge($storageSetting$set);
  225.             $this->getSettingService()->set('storage'$storageSetting);
  226.             return $this->createJsonResponse(true);
  227.         }
  228.         return $this->createJsonResponse(false);
  229.     }
  230.     public function videoWatermarkUploadAction(Request $request)
  231.     {
  232.         $file $request->files->get('watermark');
  233.         if (!FileToolkit::isImageFile($file)) {
  234.             throw $this->createAccessDeniedException('图片格式不正确!');
  235.         }
  236.         $filename 'watermark_'.time().'.'.$file->getClientOriginalExtension();
  237.         $directory "{$this->container->getParameter('topxia.upload.public_directory')}/system";
  238.         $file $file->move($directory$filename);
  239.         $path "system/{$filename}";
  240.         $response = [
  241.             'path' => $path,
  242.             'url' => $this->get('web.twig.extension')->getFileUrl($path),
  243.         ];
  244.         return new Response(json_encode($response));
  245.     }
  246.     public function videoEmbedWatermarkUploadAction(Request $request)
  247.     {
  248.         $file $request->files->get('watermark');
  249.         if (!FileToolkit::isImageFile($file)) {
  250.             throw $this->createAccessDeniedException('图片格式不正确!');
  251.         }
  252.         $filename 'watermarkembed_'.time().'.'.$file->getClientOriginalExtension();
  253.         $directory "{$this->container->getParameter('topxia.upload.public_directory')}/system";
  254.         $file $file->move($directory$filename);
  255.         $path "system/{$filename}";
  256.         $originFileInfo getimagesize($file);
  257.         $filePath $this->container->getParameter('topxia.upload.public_directory').'/'.$path;
  258.         $imagine = new Imagine();
  259.         $rawImage $imagine->open($filePath);
  260.         $pathinfo pathinfo($filePath);
  261.         $specification['240'] = 20;
  262.         $specification['360'] = 30;
  263.         $specification['480'] = 40;
  264.         $specification['720'] = 60;
  265.         $specification['1080'] = 90;
  266.         foreach ($specification as $key => $value) {
  267.             $width = ($originFileInfo[0] * $value $originFileInfo[1]);
  268.             $specialImage $rawImage->copy();
  269.             $specialImage->resize(new Box($width$value));
  270.             $filePath "{$pathinfo['dirname']}/{$pathinfo['filename']}-{$key}.{$pathinfo['extension']}";
  271.             $specialImage->save($filePath);
  272.         }
  273.         $response = [
  274.             'path' => $path,
  275.             'url' => $this->get('web.twig.extension')->getFileUrl($path),
  276.         ];
  277.         return new Response(json_encode($response));
  278.     }
  279.     public function videoWatermarkRemoveAction(Request $request)
  280.     {
  281.         return $this->createJsonResponse(true);
  282.     }
  283.     public function smsStatusAction(Request $request)
  284.     {
  285.         $dataUserPosted $request->request->all();
  286.         $settings $this->getSettingService()->get('cloud_sms', []);
  287.         try {
  288.             $api CloudAPIFactory::create('root');
  289.             $overview $api->get('/me/sms/overview');
  290.             $cloudInfo $api->get('/me');
  291.         } catch (\RuntimeException $e) {
  292.             return $this->render('admin/edu-cloud/sms-error.html.twig', []);
  293.         }
  294.         if (isset($overview['isBuy'])) {
  295.             $this->setFlashMessage('danger''site.illegal.request');
  296.         }
  297.         $smsStatus array_merge(SmsConstants::getDefaults(), $dataUserPosted);
  298.         if (empty($cloudInfo['accessCloud'])) {
  299.             return $this->createMessageResponse('info''对不起,请先接入教育云!'''3,
  300.                 $this->generateUrl('admin_my_cloud_overview'));
  301.         }
  302.         //启动
  303.         if (isset($dataUserPosted['sms-open'])) {
  304.             $smsStatus array_merge($smsStatus$settings);
  305.             $smsStatus[SmsConstants::SMS_ENABLED] = 1;
  306.         }
  307.         $status $this->getSmsService()->getSmsAccount();
  308.         if (isset($dataUserPosted['sms-close'])) {
  309.             $smsStatus array_merge($smsStatus$settings);
  310.             $smsStatus[SmsConstants::SMS_ENABLED] = 0;
  311.         }
  312.         $smsStatus['status'] = isset($status['status']) ? $status['status'] : 'error';
  313.         $this->getSettingService()->set('cloud_sms'$smsStatus);
  314.         return $this->redirect($this->generateUrl('admin_edu_cloud_sms'));
  315.     }
  316.     //云短信概览页
  317.     public function smsOverviewAction(Request $request)
  318.     {
  319.         if ($this->getWebExtension()->isTrial()) {
  320.             return $this->render('admin/edu-cloud/sms/trial.html.twig');
  321.         }
  322.         if (!($this->isVisibleCloud())) {
  323.             return $this->redirect($this->generateUrl('admin_my_cloud_overview'));
  324.         }
  325.         $settings $this->getSettingService()->get('storage', []);
  326.         if (empty($settings['cloud_access_key']) || empty($settings['cloud_secret_key'])) {
  327.             $this->setFlashMessage('warning''admin.cloud.license.has_no_license');
  328.             return $this->redirect($this->generateUrl('admin_setting_cloud_key_update'));
  329.         }
  330.         $cloudSmsSettings $this->getSettingService()->get('cloud_sms', []);
  331.         try {
  332.             $api CloudAPIFactory::create('root');
  333.             $overview $api->get('/me/sms/overview');
  334.             $smsInfo $this->getSmsService()->getSmsAccount();
  335.             $this->checkSmsSign($smsInfo);
  336.         } catch (\RuntimeException $e) {
  337.             return $this->render('admin/edu-cloud/sms-error.html.twig', []);
  338.         }
  339.         $isSmsWithoutEnable $this->isSmsWithoutEnable($overview$cloudSmsSettings);
  340.         if ($isSmsWithoutEnable) {
  341.             $overview['isBuy'] = isset($overview['isBuy']) ? $overview['isBuy'] : true;
  342.             return $this->render('admin/edu-cloud/sms/without-enable.html.twig', [
  343.                 'overview' => $overview,
  344.                 'cloudSmsSettings' => $cloudSmsSettings,
  345.             ]);
  346.         }
  347.         $chartData $this->dealChartData($overview['data']);
  348.         return $this->render('admin/edu-cloud/sms/overview.html.twig', [
  349.             'account' => $overview['account'],
  350.             'chartData' => $chartData,
  351.             'smsInfo' => $smsInfo,
  352.         ]);
  353.     }
  354.     //云短信设置
  355.     public function smsSettingAction(Request $request)
  356.     {
  357.         if ($this->getWebExtension()->isTrial()) {
  358.             return $this->render('admin/edu-cloud/sms/trial.html.twig');
  359.         }
  360.         if (!($this->isVisibleCloud())) {
  361.             return $this->redirect($this->generateUrl('admin_my_cloud_overview'));
  362.         }
  363.         $cloudSmsSettings $this->getSettingService()->get('cloud_sms', []);
  364.         if ((isset($cloudSmsSettings['sms_enabled']) && == $cloudSmsSettings['sms_enabled']) || !isset($cloudSmsSettings['sms_enabled'])) {
  365.             return $this->redirect($this->generateUrl('admin_edu_cloud_sms'));
  366.         }
  367.         try {
  368.             $api CloudAPIFactory::create('root');
  369.             if ($request->isMethod('POST')) {
  370.                 $this->handleSmsSetting($request$api);
  371.                 $this->setFlashMessage('success''site.save.success');
  372.             }
  373.             $smsInfo $this->getSmsService()->getSmsAccount();
  374.             $this->checkSmsSign($smsInfo);
  375.             $isBinded $this->getAppService()->getBinded();
  376.             return $this->render('admin/edu-cloud/sms/setting.html.twig', [
  377.                 'isBinded' => $isBinded,
  378.                 'smsInfo' => $smsInfo,
  379.             ]);
  380.         } catch (\RuntimeException $e) {
  381.             return $this->render('admin/edu-cloud/sms-error.html.twig', []);
  382.         }
  383.     }
  384.     protected function checkSmsSign($smsInfo)
  385.     {
  386.         if (empty($smsInfo)) {
  387.             $smsSignUrl $this->generateUrl('admin_cloud_sms_sign');
  388.             $this->setFlashMessage('danger',
  389.                 "尚未开通云短信,不能发送短信, <a href='{$smsSignUrl}' class='plm' target='_blank'>去设置</a>");
  390.         } else {
  391.             if (empty($smsInfo['name']) && empty($smsInfo['isExistSmsSign'])) {
  392.                 $smsSignUrl $this->generateUrl('admin_cloud_sms_sign');
  393.                 $this->setFlashMessage('danger',
  394.                     "尚未设置短信签名,不能发送短信, <a href='{$smsSignUrl}' class='plm' target='_blank'>去设置</a>");
  395.             }
  396.             if (empty($smsInfo['name']) && !empty($smsInfo['isExistSmsSign']) && null == $smsInfo['usedSmsSign']) {
  397.                 $this->setFlashMessage('danger''admin.cloud.sms.signature_in_reviewing');
  398.             }
  399.         }
  400.     }
  401.     //原云邮件设置页
  402.     public function emailAction(Request $request)
  403.     {
  404.         if ($this->getWebExtension()->isTrial()) {
  405.             return $this->render('admin/edu-cloud/email.html.twig');
  406.         }
  407.         $settings $this->getSettingService()->get('storage', []);
  408.         if (empty($settings['cloud_access_key']) || empty($settings['cloud_secret_key'])) {
  409.             $this->setFlashMessage('warning''admin.cloud.license.has_no_license');
  410.             return $this->redirect($this->generateUrl('admin_setting_cloud_key_update'));
  411.         }
  412.         try {
  413.             $api CloudAPIFactory::create('root');
  414.             $info $api->get('/me');
  415.             $status $api->get('/me/email_account');
  416.             $emailStatus $this->handleEmailSetting($request);
  417.             $overview $api->get("/user/center/{$api->getAccessKey()}/overview");
  418.             $emailInfo $emailInfo = isset($overview['service']['email']) ? $overview['service']['email'] : null;
  419.             return $this->render('admin/edu-cloud/email/overview.html.twig', [
  420.                 'locked' => isset($info['locked']) ? $info['locked'] : 0,
  421.                 'enabled' => isset($info['enabled']) ? $info['enabled'] : 1,
  422.                 'email_enable' => isset($status['status']) ? $status['status'] : 'enable',
  423.                 'accessCloud' => $this->isAccessEduCloud(),
  424.                 'emailStatus' => $emailStatus,
  425.                 'emailInfo' => $emailInfo,
  426.             ]);
  427.         } catch (\RuntimeException $e) {
  428.             return $this->render('admin/edu-cloud/email-error.html.twig', []);
  429.         }
  430.     }
  431.     public function emailOverviewAction(Request $request)
  432.     {
  433.         if ($this->getWebExtension()->isTrial()) {
  434.             return $this->render('admin/edu-cloud/email/trial.html.twig');
  435.         }
  436.         if (!($this->isVisibleCloud())) {
  437.             return $this->redirect($this->generateUrl('admin_my_cloud_overview'));
  438.         }
  439.         $settings $this->getSettingService()->get('storage', []);
  440.         if (empty($settings['cloud_access_key']) || empty($settings['cloud_secret_key'])) {
  441.             $this->setFlashMessage('warning''admin.cloud.license.has_no_license');
  442.             return $this->redirect($this->generateUrl('admin_setting_cloud_key_update'));
  443.         }
  444.         try {
  445.             $api CloudAPIFactory::create('root');
  446.             $overview $api->get('/me/email/overview');
  447.         } catch (\RuntimeException $e) {
  448.             return $this->render('admin/edu-cloud/email-error.html.twig', []);
  449.         }
  450.         $emailSettings $this->getSettingService()->get('cloud_email_crm', []);
  451.         $isEmailWithoutEnable $this->isEmailWithoutEnable($overview$emailSettings);
  452.         if ($isEmailWithoutEnable) {
  453.             $overview['isBuy'] = isset($overview['isBuy']) ? false true;
  454.             return $this->render('admin/edu-cloud/email/without-enable.html.twig', [
  455.                 'overview' => $overview,
  456.             ]);
  457.         }
  458.         $chartData $this->dealChartData($overview['data']);
  459.         return $this->render('admin/edu-cloud/email/overview.html.twig', [
  460.             'account' => $overview['account'],
  461.             'chartData' => $chartData,
  462.         ]);
  463.     }
  464.     private function isEmailWithoutEnable($overview$emailSettings)
  465.     {
  466.         $isEmailWithoutEnable = (isset($emailSettings['status']) && 'disable' == $emailSettings['status']) || !isset($emailSettings['status']) || (isset($overview['isBuy']) && false == $overview['isBuy']);
  467.         return $isEmailWithoutEnable;
  468.     }
  469.     //云邮件设置页
  470.     public function emailSettingAction(Request $request)
  471.     {
  472.         if ($this->getWebExtension()->isTrial()) {
  473.             return $this->render('admin/edu-cloud/email/trial.html.twig');
  474.         }
  475.         if (!$this->isVisibleCloud()) {
  476.             return $this->redirect($this->generateUrl('admin_my_cloud_overview'));
  477.         }
  478.         $emailSettings $this->getSettingService()->get('cloud_email_crm', []);
  479.         if (!isset($emailSettings['status']) || (isset($emailSettings['status']) && 'disable' == $emailSettings['status'])) {
  480.             return $this->redirect($this->generateUrl('admin_edu_cloud_email'));
  481.         }
  482.         $settings $this->getSettingService()->get('storage', []);
  483.         if (empty($settings['cloud_access_key']) || empty($settings['cloud_secret_key'])) {
  484.             $this->setFlashMessage('warning''admin.cloud.license.has_no_license');
  485.             return $this->redirect($this->generateUrl('admin_setting_cloud_key_update'));
  486.         }
  487.         try {
  488.             $api CloudAPIFactory::create('root');
  489.             $account $api->get('/me/email_account');
  490.             return $this->render('admin/edu-cloud/email/setting.html.twig', [
  491.                 'account' => $account,
  492.             ]);
  493.         } catch (\RuntimeException $e) {
  494.             return $this->render('admin/edu-cloud/email-error.html.twig', []);
  495.         }
  496.     }
  497.     public function emailSwitchAction(Request $request)
  498.     {
  499.         if ('POST' == $request->getMethod()) {
  500.             try {
  501.                 $api CloudAPIFactory::create('root');
  502.                 $overview $api->get('/me/email/overview');
  503.             } catch (\RuntimeException $e) {
  504.                 return $this->render('admin/edu-cloud/email-error.html.twig', []);
  505.             }
  506.             if (isset($overview['isBuy'])) {
  507.                 $this->setFlashMessage('danger''site.illegal.request');
  508.             }
  509.             $status $request->request->all();
  510.             if (isset($status['email-open'])) {
  511.                 $emailStatus['status'] = 'enable';
  512.                 $this->getSettingService()->set('cloud_email_crm'$emailStatus);
  513.                 $mailer $this->getSettingService()->get('mailer');
  514.                 $mailer['enabled'] = 0;
  515.                 $this->getSettingService()->set('mailer'$mailer);
  516.             }
  517.             if (isset($status['email-close'])) {
  518.                 $emailStatus['status'] = 'disable';
  519.                 $this->getSettingService()->set('cloud_email_crm'$emailStatus);
  520.             }
  521.             return $this->redirect($this->generateUrl('admin_edu_cloud_email'));
  522.         }
  523.     }
  524.     public function applyForSmsAction(Request $request)
  525.     {
  526.         if ('POST' == $request->getMethod()) {
  527.             $result null;
  528.             $dataUserPosted $request->request->all();
  529.             if (
  530.                 isset($dataUserPosted['name'])
  531.                 && ($this->calStrlen($dataUserPosted['name']) >= 2)
  532.                 && ($this->calStrlen($dataUserPosted['name']) <= 16)
  533.             ) {
  534.                 $api CloudAPIFactory::create('root');
  535.                 $result $api->post("/sms/{$api->getAccessKey()}/apply", ['name' => $dataUserPosted['name']]);
  536.                 if (isset($result['status']) && ('ok' == $result['status'])) {
  537.                     $this->setCloudSmsKey(SmsConstants::SMS_SCHOOL_CANDIDATE_NAME$dataUserPosted['name']);
  538.                     $this->setCloudSmsKey(SmsConstants::SHOW_MESSAGE'on');
  539.                     return $this->createJsonResponse(['ACK' => 'ok']);
  540.                 }
  541.             }
  542.             return $this->createJsonResponse([
  543.                 'ACK' => 'failed',
  544.                 'message' => $result['error'].'|'.($this->calStrlen($dataUserPosted['name'])),
  545.             ]);
  546.         }
  547.         return $this->render('admin/edu-cloud/apply-sms-form.html.twig', []);
  548.     }
  549.     public function smsNoMessageAction(Request $request)
  550.     {
  551.         $this->setCloudSmsKey(SmsConstants::SHOW_MESSAGE'off');
  552.         return $this->redirect($this->generateUrl('admin_edu_cloud_sms', []));
  553.     }
  554.     //授权页
  555.     public function keyAction(Request $request)
  556.     {
  557.         $settings $this->getSettingService()->get('storage', []);
  558.         if (empty($settings['cloud_access_key']) || empty($settings['cloud_secret_key'])) {
  559.             return $this->redirect($this->generateUrl('admin_setting_cloud_key_update'));
  560.         }
  561.         $info = [];
  562.         try {
  563.             $api CloudAPIFactory::create('root');
  564.             $info $api->get('/me');
  565.         } catch (\RuntimeException $e) {
  566.             $info['error'] = 'error';
  567.         }
  568.         if (isset($info['error'])) {
  569.             $this->refreshCloudSchoolSetting($info);
  570.         }
  571.         return $this->render('admin/edu-cloud/key.html.twig', [
  572.             'info' => $info,
  573.         ]);
  574.     }
  575.     public function keyInfoAction(Request $request)
  576.     {
  577.         $api CloudAPIFactory::create('root');
  578.         $info $api->get('/me');
  579.         if (!empty($info['accessKey'])) {
  580.             $settings $this->getSettingService()->get('storage', []);
  581.             if (empty($settings['cloud_key_applied'])) {
  582.                 $settings['cloud_key_applied'] = 1;
  583.                 $this->getSettingService()->set('storage'$settings);
  584.             }
  585.             if ($info['copyright']) {
  586.                 $copyright $this->getSettingService()->get('copyright', []);
  587.                 $copyright['owned'] = 1;
  588.                 $copyright['thirdCopyright'] = $info['thirdCopyright'];
  589.                 $copyright['licenseDomains'] = $info['licenseDomains'];
  590.                 $this->getSettingService()->set('copyright'$copyright);
  591.             } else {
  592.                 $this->getSettingService()->delete('copyright');
  593.             }
  594.             $this->getLmsService()->refreshSiteName();
  595.         } else {
  596.             $settings $this->getSettingService()->get('storage', []);
  597.             $settings['cloud_key_applied'] = 0;
  598.             $this->getSettingService()->set('storage'$settings);
  599.         }
  600.         $currentHost $request->server->get('HTTP_HOST');
  601.         if (isset($info['licenseDomains'])) {
  602.             $info['licenseDomainCount'] = count(explode(';'$info['licenseDomains']));
  603.         }
  604.         return $this->render('admin/edu-cloud/key-license-info.html.twig', [
  605.             'info' => $info,
  606.             'currentHost' => $currentHost,
  607.             'isLocalAddress' => $this->isLocalAddress($currentHost),
  608.         ]);
  609.     }
  610.     public function keyBindAction(Request $request)
  611.     {
  612.         $api CloudAPIFactory::create('root');
  613.         $currentHost $request->server->get('HTTP_HOST');
  614.         $result $api->post('/me/license-domain', ['domain' => $currentHost]);
  615.         if (!empty($result['licenseDomains'])) {
  616.             $this->setFlashMessage('success''admin.cloud.license.activate_success');
  617.         } else {
  618.             $this->setFlashMessage('danger''admin.cloud.license.activate_fail');
  619.         }
  620.         return $this->createJsonResponse($result);
  621.     }
  622.     public function keyUpdateAction(Request $request)
  623.     {
  624.         if ($this->getWebExtension()->isTrial()) {
  625.             return $this->redirect($this->generateUrl('admin_setting_cloud_key'));
  626.         }
  627.         $settings $this->getSettingService()->get('storage', []);
  628.         if ('POST' == $request->getMethod()) {
  629.             $options $request->request->all();
  630.             $api CloudAPIFactory::create('root');
  631.             $api->setKey($options['accessKey'], $options['secretKey']);
  632.             $result $api->post(sprintf('/keys/%s/verification'$options['accessKey']));
  633.             if (isset($result['error'])) {
  634.                 $this->setFlashMessage('danger''admin.cloud.license.incorrect');
  635.                 goto render;
  636.             }
  637.             $user $api->get('/me');
  638.             if ('training' != $user['edition']) {
  639.                 $this->setFlashMessage('danger''admin.cloud.license.edition_mismatching');
  640.                 goto render;
  641.             }
  642.             $settings['cloud_access_key'] = $options['accessKey'];
  643.             $settings['cloud_secret_key'] = $options['secretKey'];
  644.             $settings['cloud_key_applied'] = 1;
  645.             $this->getSettingService()->set('storage'$settings);
  646.             $this->setFlashMessage('success''site.save.success');
  647.             return $this->redirect($this->generateUrl('admin_setting_cloud_key'));
  648.         }
  649.         render:
  650.         return $this->render('admin/edu-cloud/key-update.html.twig', []);
  651.     }
  652.     public function searchSettingAction(Request $request)
  653.     {
  654.         if ($this->getWebExtension()->isTrial()) {
  655.             return $this->render('admin/edu-cloud/search/trial.html.twig');
  656.         }
  657.         if (!($this->isVisibleCloud())) {
  658.             return $this->redirect($this->generateUrl('admin_my_cloud_overview'));
  659.         }
  660.         $cloudSearchSettting $this->getSettingService()->get('cloud_search', []);
  661.         if (!$cloudSearchSettting['search_enabled']) {
  662.             return $this->redirect($this->generateUrl('admin_edu_cloud_search'));
  663.         }
  664.         $cloudSearchSetting $this->getSettingService()->get('cloud_search', []);
  665.         $searchInitStatus $this->checkCloudSearchStatus($cloudSearchSetting);
  666.         return $this->render('admin/edu-cloud/search/setting.html.twig', [
  667.             'searchInitStatus' => $searchInitStatus,
  668.         ]);
  669.     }
  670.     protected function checkCloudSearchStatus($cloudSearchSetting)
  671.     {
  672.         if ('waiting' == $cloudSearchSetting['status']) {
  673.             $api CloudAPIFactory::create('root');
  674.             $search_account $api->get('/me/search_account');
  675.             if ('yes' == $search_account['isInit']) {
  676.                 $searchInitStatus 'init';
  677.             } else {
  678.                 $searchInitStatus 'notInit';
  679.             }
  680.         }
  681.         if ('ok' == $cloudSearchSetting['status']) {
  682.             $searchInitStatus 'init';
  683.         }
  684.         return isset($searchInitStatus) ? $searchInitStatus '';
  685.     }
  686.     public function searchOverviewAction(Request $request)
  687.     {
  688.         if ($this->getWebExtension()->isTrial()) {
  689.             return $this->render('admin/edu-cloud/search/trial.html.twig');
  690.         }
  691.         if (!$this->isVisibleCloud()) {
  692.             return $this->redirect($this->generateUrl('admin_my_cloud_overview'));
  693.         }
  694.         $cloud_search_setting $this->getSettingService()->get('cloud_search', []);
  695.         try {
  696.             $api CloudAPIFactory::create('root');
  697.             $userOverview $api->get("/users/{$api->getAccessKey()}/overview");
  698.             $searchOverview $api->get('/me/search/overview');
  699.             $data $this->initCloudSearch($api$cloud_search_setting);
  700.         } catch (\RuntimeException $e) {
  701.             return $this->render('admin/edu-cloud/search/without-enable.html.twig', [
  702.                 'data' => ['status' => 'unlink'],
  703.             ]);
  704.         }
  705.         //判断云搜索状态
  706.         if (empty($userOverview['user']['licenseDomains'])) {
  707.             $data['status'] = 'unbinded';
  708.         } else {
  709.             $currentHost $request->server->get('HTTP_HOST');
  710.             if (!in_array($currentHostexplode(';'$userOverview['user']['licenseDomains']), true)) {
  711.                 $data['status'] = 'binded_error';
  712.             }
  713.         }
  714.         if (!isset($searchOverview['isBuy']) && == $data['search_enabled'] && ('ok' == $data['status'] || 'waiting' == $data['status'])) {
  715.             $chartData $this->dealChartData($searchOverview['data']);
  716.             return $this->render('admin/edu-cloud/search/overview.html.twig', [
  717.                 'searchOverview' => $searchOverview,
  718.                 'chartData' => $chartData,
  719.             ]);
  720.         } else {
  721.             return $this->render('admin/edu-cloud/search/without-enable.html.twig', [
  722.                 'data' => $data,
  723.             ]);
  724.         }
  725.     }
  726.     public function searchReapplyAction(Request $request)
  727.     {
  728.         if ('POST' == $request->getMethod()) {
  729.             $callbackRouteUrl $this->generateUrl('edu_cloud_search_callback');
  730.             $this->getSearchService()->applySearchAccount($callbackRouteUrl);
  731.             $this->getSearchService()->refactorAllDocuments();
  732.             return $this->redirect($this->generateUrl('admin_edu_cloud_search'));
  733.         }
  734.         return $this->render('admin/edu-cloud/cloud-search-reapply-modal.html.twig');
  735.     }
  736.     public function searchClauseAction(Request $request)
  737.     {
  738.         if ('POST' == $request->getMethod()) {
  739.             $callbackRouteUrl $this->generateUrl('edu_cloud_search_callback');
  740.             $this->getSearchService()->applySearchAccount($callbackRouteUrl);
  741.             return $this->redirect($this->generateUrl('admin_edu_cloud_search'));
  742.         }
  743.         return $this->render('admin/edu-cloud/cloud-search-clause-modal.html.twig');
  744.     }
  745.     public function searchOpenAction()
  746.     {
  747.         $cloud_search_setting $this->getSettingService()->get('cloud_search', []);
  748.         if ('ok' == $cloud_search_setting['status'] || 'waiting' == $cloud_search_setting['status']) {
  749.             $cloud_search_setting['search_enabled'] = 1;
  750.             $this->getSettingService()->set('cloud_search'$cloud_search_setting);
  751.         }
  752.         return $this->redirect($this->generateUrl('admin_edu_cloud_search'));
  753.     }
  754.     public function searchCloseAction()
  755.     {
  756.         $cloud_search_setting $this->getSettingService()->get('cloud_search', []);
  757.         $cloud_search_setting['search_enabled'] = 0;
  758.         $this->getSettingService()->set('cloud_search'$cloud_search_setting);
  759.         return $this->redirect($this->generateUrl('admin_edu_cloud_search'));
  760.     }
  761.     public function setSearchResultTypeAction(Request $request)
  762.     {
  763.         $newSetting $request->query->all();
  764.         $cloud_search_setting $this->getSettingService()->get('cloud_search');
  765.         $differentSetting array_diff_assoc($cloud_search_setting['type'], $newSetting);
  766.         foreach ($cloud_search_setting['type'] as $key => &$type) {
  767.             $type 1;
  768.             if ('course' !== (string) $key && array_key_exists($key$differentSetting)) {
  769.                 $type 0;
  770.             }
  771.         }
  772.         $this->getSettingService()->set('cloud_search'$cloud_search_setting);
  773.         return $this->redirect($this->generateUrl('admin_edu_cloud_setting_search'));
  774.     }
  775.     public function keyApplyAction(Request $request)
  776.     {
  777.         $applier = new KeyApplier();
  778.         $keys $applier->applyKey($this->getUser());
  779.         if (empty($keys['accessKey']) || empty($keys['secretKey'])) {
  780.             return $this->createJsonResponse(['error' => 'Key生成失败,请检查服务器网络后,重试!']);
  781.         }
  782.         $settings $this->getSettingService()->get('storage', []);
  783.         $settings['cloud_access_key'] = $keys['accessKey'];
  784.         $settings['cloud_secret_key'] = $keys['secretKey'];
  785.         $settings['cloud_key_applied'] = 1;
  786.         $this->getSettingService()->set('storage'$settings);
  787.         return $this->createJsonResponse(['status' => 'ok']);
  788.     }
  789.     public function keyCopyrightAction(Request $request)
  790.     {
  791.         $api CloudAPIFactory::create('leaf');
  792.         $info $api->get('/me');
  793.         if (empty($info['copyright'])) {
  794.             throw $this->createAccessDeniedException('您无权操作!');
  795.         }
  796.         $name $request->request->get('name');
  797.         $this->getSettingService()->set('copyright', [
  798.             'owned' => 1,
  799.             'name' => $request->request->get('name'''),
  800.             'thirdCopyright' => isset($info['thirdCopyright']) ? $info['thirdCopyright'] : 0,
  801.             'licenseDomains' => isset($info['licenseDomains']) ? $info['licenseDomains'] : '',
  802.         ]);
  803.         $this->getLmsService()->refreshSiteName();
  804.         return $this->createJsonResponse(['status' => 'ok']);
  805.     }
  806.     public function appImAction(Request $request)
  807.     {
  808.         $appImSetting $this->getSettingService()->get('app_im', []);
  809.         if (!$appImSetting) {
  810.             $appImSetting = ['enabled' => 0'convNo' => ''];
  811.             $this->getSettingService()->set('app_im'$appImSetting);
  812.         }
  813.         $data = ['status' => 'success'];
  814.         try {
  815.             $api CloudAPIFactory::create('root');
  816.             $overview $api->get("/users/{$api->getAccessKey()}/overview");
  817.         } catch (\RuntimeException $e) {
  818.             return $this->render('admin/edu-cloud/video-error.html.twig', []);
  819.         }
  820.         //是否接入教育云
  821.         if (empty($overview['user']['level']) || (!(isset($overview['service']['storage'])) && !(isset($overview['service']['live'])) && !(isset($overview['service']['sms'])))) {
  822.             $data['status'] = 'unconnect';
  823.         } elseif (empty($overview['user']['licenseDomains'])) {
  824.             $data['status'] = 'unbinded';
  825.         } else {
  826.             $currentHost $request->server->get('HTTP_HOST');
  827.             if (!in_array($currentHostexplode(';'$overview['user']['licenseDomains']))) {
  828.                 $data['status'] = 'binded_error';
  829.             }
  830.         }
  831.         return $this->render('admin/edu-cloud/app-im-setting.html.twig', [
  832.             'data' => $data,
  833.         ]);
  834.     }
  835.     public function appImUpdateStatusAction(Request $request)
  836.     {
  837.         if ('POST' == $request->getMethod()) {
  838.             $appImSetting $this->getSettingService()->get('app_im', []);
  839.             $user $this->getUser();
  840.             //去云平台判断im帐号是否存在
  841.             $api IMAPIFactory::create();
  842.             $imAccount $api->get('/me/account');
  843.             if (isset($imAccount['error']) || empty($imAccount['account'])) {
  844.                 $imAccount $api->post('/accounts');
  845.             }
  846.             $status $request->request->get('status'0);
  847.             $imStatus $status 'enable' 'disable';
  848.             //更改云IM帐号状态
  849.             $api->post('/me/account', ['status' => $imStatus]);
  850.             $appImSetting['enabled'] = $status;
  851.             //创建全站会话
  852.             if ($status && empty($appImSetting['convNo'])) {
  853.                 $conversation $this->getConversationService()->createConversation('全站会话''global'0, [$user]);
  854.                 if ($conversation) {
  855.                     $appImSetting['convNo'] = $conversation['no'];
  856.                 }
  857.                 $this->createOrgConversation();
  858.             }
  859.             $this->getSettingService()->set('app_im'$appImSetting);
  860.             return $this->createJsonResponse(true);
  861.         }
  862.         return $this->createJsonResponse(false);
  863.     }
  864.     protected function createOrgConversation()
  865.     {
  866.         $user $this->getCurrentUser();
  867.         $targetIds ArrayToolkit::column($this->getOrgService()->findOrgsByPrefixOrgCode('1.'), 'id');
  868.         $conversations $this->getConversationService()->searchConversations(['targetIds' => $targetIds'targetType' => 'global'], [], 0PHP_INT_MAX);
  869.         $conversations ArrayToolkit::index($conversations'targetId');
  870.         foreach ($targetIds as $targetId) {
  871.             if (!isset($conversations[$targetId])) {
  872.                 $this->getConversationService()->createConversation('部门会话''global'$targetId, [$user]);
  873.             }
  874.         }
  875.     }
  876.     protected function initCloudSearch(AbstractCloudAPI $api$data)
  877.     {
  878.         if (!$data) {
  879.             $data = [
  880.                 'search_enabled' => 0,
  881.                 'status' => 'closed'//'closed':未开启;'waiting':'索引中';'ok':'索引完成'
  882.             ];
  883.         }
  884.         if (empty($data['status'])) {
  885.             $data['status'] = 'closed';
  886.         }
  887.         if ('waiting' == $data['status']) {
  888.             $search_account $api->get('/me/search_account');
  889.             if ('yes' == $search_account['isInit']) {
  890.                 $data = [
  891.                     'search_enabled' => $data['search_enabled'],
  892.                     'status' => 'ok',
  893.                 ];
  894.             }
  895.         }
  896.         if (empty($data['type'])) {
  897.             $data['type'] = [
  898.                 'course' => 1,
  899.                 'classroom' => 1,
  900.                 'teacher' => 1,
  901.                 'thread' => 1,
  902.                 'article' => 1,
  903.             ];
  904.         }
  905.         $this->getSettingService()->set('cloud_search'$data);
  906.         return $data;
  907.     }
  908.     protected function dateFormat($time)
  909.     {
  910.         return strtotime(substr($time04).'-'.substr($time42));
  911.     }
  912.     protected function monthDays($time)
  913.     {
  914.         return date('t'strtotime("{$time}-1"));
  915.     }
  916.     private function isSmsWithoutEnable($overview$cloudSmsSettings)
  917.     {
  918.         $isSmsWithoutEnable = (isset($overview['isBuy']) && false == $overview['isBuy']) || (isset($cloudSmsSettings['sms_enabled']) && == $cloudSmsSettings['sms_enabled']) || !isset($cloudSmsSettings['sms_enabled']);
  919.         return $isSmsWithoutEnable;
  920.     }
  921.     protected function handleSmsSetting(Request $request$api)
  922.     {
  923.         $dataUserPosted $request->request->all();
  924.         $settings $this->getSettingService()->get('cloud_sms', []);
  925.         if (empty($settings)) {
  926.             $smsStatus array_merge(SmsConstants::getDefaults(), $dataUserPosted);
  927.             $status $this->getSmsService()->getSmsAccount();
  928.             $smsStatus['status'] = isset($status['status']) ? $status['status'] : 'error';
  929.             $this->getSettingService()->set('cloud_sms'$smsStatus);
  930.         }
  931.     }
  932.     protected function handleEmailSetting(Request $request)
  933.     {
  934.         $dataUserPosted $request->request->all();
  935.         list($emailStatus$sign) = $this->getSign($dataUserPosted);
  936.         $emailStatus array_merge($emailStatus$sign);
  937.         if ('error' != $emailStatus['status'] && !empty($dataUserPosted)) {
  938.             $this->getSettingService()->set('cloud_email_crm'$emailStatus);
  939.         }
  940.         $emailStatus $this->getSettingService()->get('cloud_email_crm', []);
  941.         return $emailStatus;
  942.     }
  943.     protected function getSign($operation)
  944.     {
  945.         $api CloudAPIFactory::create('root');
  946.         $settings $this->getSettingService()->get('cloud_email_crm', []);
  947.         $result = [];
  948.         $sign = [];
  949.         $emailStatus = [];
  950.         if (isset($operation['email-open'])) {
  951.             $status $api->get('/me/email_account');
  952.             if (isset($status['error']) && 101 == $status['error']['code']) {
  953.                 $site $this->getSettingService()->get('site', []);
  954.                 $result $api->post('/email_accounts',
  955.                     ['sender' => isset($site['name']) ? $site['name'] : '我的网校']);
  956.                 if (isset($result['status']) && 'enable' == $result['status']) {
  957.                     $emailStatus['status'] = 'enable';
  958.                     $emailStatus array_merge($settings$emailStatus);
  959.                     $sign = ['sign' => $result['nickname']];
  960.                 }
  961.             } else {
  962.                 $emailStatus['status'] = 'enable';
  963.                 $emailStatus array_merge($settings$emailStatus);
  964.                 $sign = ['sign' => $settings['sign']];
  965.             }
  966.             $result $api->get('/me/email_account');
  967.             $this->setFlashMessage('success''site.save.success');
  968.             $mailer $this->getSettingService()->get('mailer', []);
  969.             if (isset($result['status']) && 'enable' == $result['status'] && '1' == $mailer['enabled']) {
  970.                 $default = [
  971.                     'enabled' => 0,
  972.                     'host' => '',
  973.                     'port' => '',
  974.                     'username' => '',
  975.                     'password' => '',
  976.                     'from' => '',
  977.                     'name' => '',
  978.                 ];
  979.                 $mailer array_merge($default$mailer);
  980.                 $mailer['enabled'] = 0;
  981.                 $this->getSettingService()->set('mailer'$mailer);
  982.                 $mailerWithoutPassword $mailer;
  983.                 $mailerWithoutPassword['password'] = '******';
  984.                 $this->getLogService()->info('system''update_settings''开启云邮件关闭第三方邮件服务器设置'$mailerWithoutPassword);
  985.             }
  986.         }
  987.         if (isset($operation['sign'])) {
  988.             if (!empty($operation['sign'])) {
  989.                 $params = [
  990.                     'sender' => $operation['sign'],
  991.                 ];
  992.                 $result $api->post('/me/email_account'$params);
  993.                 if (isset($result['nickname'])) {
  994.                     $this->setFlashMessage('success''site.save.success');
  995.                     $emailStatus['status'] = $settings['status'];
  996.                     $sign = ['sign' => $result['nickname']];
  997.                 } else {
  998.                     $this->setFlashMessage('danger''site.save.fail');
  999.                 }
  1000.             } else {
  1001.                 $emailStatus['status'] = $settings['status'];
  1002.                 $sign = ['sign' => $settings['sign']];
  1003.             }
  1004.         }
  1005.         if (isset($operation['email-close'])) {
  1006.             $emailStatus['status'] = 'disable';
  1007.             $emailStatus array_merge($settings$emailStatus);
  1008.             $this->setFlashMessage('success''site.save.success');
  1009.         }
  1010.         if (empty($operation)) {
  1011.             $result $api->get('/me/email_account');
  1012.             if (isset($result['nickname'])) {
  1013.                 $emailStatus['status'] = $result['status'];
  1014.                 $sign = ['sign' => $result['nickname']];
  1015.             } else {
  1016.                 $emailStatus['status'] = 'disable';
  1017.                 $sign = isset($settings['sign']) ? ['sign' => $settings['sign']] : ['sign' => ''];
  1018.             }
  1019.         }
  1020.         if (isset($result['error'])) {
  1021.             $emailStatus['status'] = 'error';
  1022.             $emailStatus['msg'] = $result['error'];
  1023.         }
  1024.         return [$emailStatus$sign];
  1025.     }
  1026.     protected function calStrlen($str)
  1027.     {
  1028.         return (strlen($str) + mb_strlen($str'UTF8')) / 2;
  1029.     }
  1030.     protected function setCloudSmsKey($key$val)
  1031.     {
  1032.         $setting $this->getSettingService()->get('cloud_sms', []);
  1033.         $setting[$key] = $val;
  1034.         $this->getSettingService()->set('cloud_sms'$setting);
  1035.     }
  1036.     protected function generateVideoChartData($info)
  1037.     {
  1038.         if (empty($info)) {
  1039.             $info = [];
  1040.         }
  1041.         $chartInfo = [];
  1042.         foreach ($info as $key => $value) {
  1043.             $chartInfo[] = '{"date":"'.$value['date'].'","spacecount":"'.$value['space'].'","transfercount":"'.$value['transfer'].'"}';
  1044.         }
  1045.         return '['.implode(','$chartInfo).']';
  1046.     }
  1047.     protected function generateChartData($info)
  1048.     {
  1049.         if (empty($info)) {
  1050.             $info = [];
  1051.         }
  1052.         $chartInfo = [];
  1053.         foreach ($info as $key => $value) {
  1054.             $chartInfo[] = '{"date":"'.$key.'","count":'.$value.'}';
  1055.         }
  1056.         return '['.implode(','$chartInfo).']';
  1057.     }
  1058.     protected function isLocalAddress($address)
  1059.     {
  1060.         if (in_array($address, ['localhost''127.0.0.1'])) {
  1061.             return true;
  1062.         }
  1063.         if (=== strpos($address'192.168.')) {
  1064.             return true;
  1065.         }
  1066.         if (=== strpos($address'10.')) {
  1067.             return true;
  1068.         }
  1069.         return false;
  1070.     }
  1071.     protected function getCloudCenterExperiencePage()
  1072.     {
  1073.         $trial file_get_contents('http://open.edusoho.com/api/v1/block/experience');
  1074.         $trialHtml json_decode($trialtrue);
  1075.         return $trialHtml;
  1076.     }
  1077.     protected function isAccessEduCloud()
  1078.     {
  1079.         try {
  1080.             $api CloudAPIFactory::create('root');
  1081.             $info $api->get('/me');
  1082.             return isset($info['accessCloud']) ? $info['accessCloud'] : 0;
  1083.         } catch (\RuntimeException $e) {
  1084.             return $this->render('admin/edu-cloud/cloud-error.html.twig', []);
  1085.         }
  1086.     }
  1087.     protected function isSearchInited($api$data)
  1088.     {
  1089.         if (!$data) {
  1090.             $data = [
  1091.                 'search_enabled' => 0,
  1092.                 'status' => 'closed'//'closed':未开启;'waiting':'索引中';'ok':'索引完成'
  1093.             ];
  1094.         }
  1095.         if ('waiting' == $data['status']) {
  1096.             $search_account $api->get('/me/search_account');
  1097.             if ('yes' == $search_account['isInit']) {
  1098.                 $data = [
  1099.                     'search_enabled' => $data['search_enabled'],
  1100.                     'status' => 'ok',
  1101.                 ];
  1102.             }
  1103.         }
  1104.         $this->getSettingService()->set('cloud_search'$data);
  1105.         return $data;
  1106.     }
  1107.     protected function getImUsedInfo()
  1108.     {
  1109.         $api IMAPIFactory::create();
  1110.         $endTime strtotime(date('Y-m-d'strtotime('-1 day')).' 23:59:59');
  1111.         $startTime strtotime(date('Y-m-d'strtotime('-6 days'$endTime)).'00:00:00');
  1112.         try {
  1113.             $imUsedInfo $api->get('/me/receive_count_period', [
  1114.                 'startTime' => $startTime,
  1115.                 'endTime' => $endTime,
  1116.             ]);
  1117.             if (isset($imUsedInfo['error'])) {
  1118.                 return [];
  1119.             }
  1120.         } catch (\RuntimeException $e) {
  1121.             return [];
  1122.         }
  1123.         $chartInfo = [];
  1124.         foreach ($imUsedInfo as $value) {
  1125.             $chartInfo[] = ['date' => $value['sendTime'], 'count' => $value['nums']];
  1126.         }
  1127.         return $chartInfo;
  1128.     }
  1129.     protected function createGlobalImConversation()
  1130.     {
  1131.         $user $this->getUser();
  1132.         $message = [
  1133.             'name' => '站点会话',
  1134.             'clients' => [
  1135.                 [
  1136.                     'clientId' => $user['id'],
  1137.                     'clientName' => $user['nickname'],
  1138.                 ],
  1139.             ],
  1140.         ];
  1141.         $result IMAPIFactory::create()->post('/me/conversation'$message);
  1142.         if (isset($result['error'])) {
  1143.             return '';
  1144.         }
  1145.         return $result['no'];
  1146.     }
  1147.     private function dealChartData($data)
  1148.     {
  1149.         $chartData['unit'] = $data['unit'];
  1150.         if (empty($data['items'])) {
  1151.             for ($i 7$i 0; --$i) {
  1152.                 $chartData['date'][] = date('Y-m-d'strtotime('-'.$i.'days'));
  1153.                 $chartData['count'][] = 0;
  1154.             }
  1155.             return $chartData;
  1156.         }
  1157.         foreach ($data['items'] as $item) {
  1158.             $chartData['date'][] = $item['date'];
  1159.             $chartData['count'][] = $item['count'];
  1160.         }
  1161.         return $chartData;
  1162.     }
  1163.     protected function getSearchService()
  1164.     {
  1165.         return $this->createService('Search:SearchService');
  1166.     }
  1167.     protected function getEduCloudService()
  1168.     {
  1169.         return $this->createService('CloudPlatform:EduCloudService');
  1170.     }
  1171.     /**
  1172.      * @return AppService
  1173.      */
  1174.     protected function getAppService()
  1175.     {
  1176.         return $this->createService('CloudPlatform:AppService');
  1177.     }
  1178.     protected function getSettingService()
  1179.     {
  1180.         return $this->createService('System:SettingService');
  1181.     }
  1182.     protected function getUploadFileService()
  1183.     {
  1184.         return $this->createService('File:UploadFileService');
  1185.     }
  1186.     protected function getSignEncoder()
  1187.     {
  1188.         return new MessageDigestPasswordEncoder('sha256');
  1189.     }
  1190.     protected function getConversationService()
  1191.     {
  1192.         return $this->createService('IM:ConversationService');
  1193.     }
  1194.     // 添加云直播
  1195.     public function liveOverviewAction(Request $request)
  1196.     {
  1197.         if ($this->getWebExtension()->isTrial()) {
  1198.             return $this->render('admin/edu-cloud/live/trial.html.twig');
  1199.         }
  1200.         if (!($this->isVisibleCloud())) {
  1201.             return $this->redirect($this->generateUrl('admin_my_cloud_overview'));
  1202.         }
  1203.         try {
  1204.             $api CloudAPIFactory::create('root');
  1205.             $overview $api->get('/me/live/overview');
  1206.             $info $api->get('/me/liveaccount', []);
  1207.             if (isset($info['provider'])) {
  1208.                 $info['requestTime'] = time();
  1209.                 $this->getSettingService()->set('cloud_live'ArrayToolkit::parts($info, ['provider''replay''type''liveProvider''requestTime']));
  1210.             }
  1211.         } catch (\RuntimeException $e) {
  1212.             return $this->render('admin/edu-cloud/live-error.html.twig', []);
  1213.         }
  1214.         $liveCourseSetting $this->getSettingService()->get('live-course', []);
  1215.         $liveEnabled = isset($liveCourseSetting['live_course_enabled']) ? $liveCourseSetting['live_course_enabled'] : 0;
  1216.         $isLiveWithoutEnable $this->isLiveWithoutEnable($overview$liveEnabled);
  1217.         if ($isLiveWithoutEnable) {
  1218.             $overview['isBuy'] = isset($overview['isBuy']) ? $overview['isBuy'] : true;
  1219.             return $this->render('admin/edu-cloud/live/without-enable.html.twig', [
  1220.                 'overview' => $overview,
  1221.             ]);
  1222.         }
  1223.         $chartData $this->dealChartData($overview['data']);
  1224.         return $this->render('admin/edu-cloud/live/overview.html.twig', [
  1225.             'account' => $overview['account'],
  1226.             'chartData' => $chartData,
  1227.         ]);
  1228.     }
  1229.     private function isLiveWithoutEnable($overview$liveEnabled)
  1230.     {
  1231.         $isLiveWithoutEnable = (isset($overview['isBuy']) && false == $overview['isBuy']) || == $liveEnabled || !isset($liveEnabled);
  1232.         return $isLiveWithoutEnable;
  1233.     }
  1234.     private function isVisibleCloud()
  1235.     {
  1236.         return $this->getEduCloudService()->isVisibleCloud();
  1237.     }
  1238.     public function liveSettingAction(Request $request)
  1239.     {
  1240.         if ($this->getWebExtension()->isTrial()) {
  1241.             return $this->render('admin/edu-cloud/live/trial.html.twig');
  1242.         }
  1243.         if (!$this->isVisibleCloud()) {
  1244.             return $this->redirect($this->generateUrl('admin_my_cloud_overview'));
  1245.         }
  1246.         $liveCourseSetting $this->getSettingService()->get('live-course', []);
  1247.         $client = new EdusohoLiveClient();
  1248.         $capacity $client->getCapacity();
  1249.         if ($request->isMethod('POST')) {
  1250.             try {
  1251.                 $api CloudAPIFactory::create('root');
  1252.                 $overview $api->get('/me/live/overview');
  1253.                 (new EdusohoLiveClient())->updateLiveCallbackUrl();
  1254.             } catch (\RuntimeException $e) {
  1255.                 return $this->render('admin/edu-cloud/live-error.html.twig', []);
  1256.             }
  1257.             if (isset($overview['isBuy'])) {
  1258.                 $this->setFlashMessage('danger''site.illegal.request');
  1259.             }
  1260.             $live $request->request->all();
  1261.             $liveCourseSetting array_merge($liveCourseSetting$live);
  1262.             $liveCourseSetting['live_student_capacity'] = empty($capacity['capacity']) ? $capacity['capacity'];
  1263.             $courseSetting $this->getSettingService()->get('course', []);
  1264.             $setting array_merge($courseSetting$liveCourseSetting);
  1265.             $this->getSettingService()->set('live-course'$liveCourseSetting);
  1266.             $this->getSettingService()->set('course'$setting);
  1267.             $this->getLogService()->info('system''update_live_settings''更新云直播设置'$setting);
  1268.             $this->setCloudLiveLogo($capacity['provider'], $client);
  1269.             $redirectUrl 'talkFun' == $capacity['provider'] ? 'admin_setting_cloud_edulive' 'admin_cloud_edulive_overview';
  1270.             return $this->redirect($this->generateUrl($redirectUrl));
  1271.         }
  1272.         if (empty($liveCourseSetting['live_course_enabled'])) {
  1273.             return $this->redirect($this->generateUrl('admin_cloud_edulive_overview'));
  1274.         }
  1275.         $liveEnabled $liveCourseSetting['live_course_enabled'];
  1276.         if (null === $liveEnabled || === $liveEnabled) {
  1277.             return $this->redirect($this->generateUrl('admin_cloud_edulive_overview'));
  1278.         }
  1279.         try {
  1280.             $api CloudAPIFactory::create('root');
  1281.             $overview $api->get('/me/live/overview');
  1282.         } catch (\RuntimeException $e) {
  1283.             return $this->render('admin/edu-cloud/live-error.html.twig', []);
  1284.         }
  1285.         return $this->render('admin/edu-cloud/live/setting.html.twig', [
  1286.             'account' => $overview['account'],
  1287.             'liveCourseSetting' => $liveCourseSetting,
  1288.             'capacity' => $capacity,
  1289.         ]);
  1290.     }
  1291.     public function logoCropAction(Request $request$type)
  1292.     {
  1293.         if (!in_array($type, ['web''app'])) {
  1294.             return $this->createMessageResponse('error''参数不正确');
  1295.         }
  1296.         if ('POST' === $request->getMethod()) {
  1297.             $options $request->request->all();
  1298.             $image $options['images'][0];
  1299.             $file $this->getFileService()->getFile($image['id']);
  1300.             $liveSetting $this->getSettingService()->get('live-course', []);
  1301.             $url $this->get('web.twig.extension')->getFurl($file['uri']);
  1302.             $oldFileId = empty($liveSetting["{$type}LogoFileId"]) ? '' $liveSetting["{$type}LogoFileId"];
  1303.             if ($oldFileId) {
  1304.                 $this->getFileService()->deleteFile($oldFileId);
  1305.             }
  1306.             $liveSetting["{$type}LogoFileId"] = $file['id'];
  1307.             $liveSetting["{$type}LogoPath"] = $url;
  1308.             $this->getSettingService()->set('live-course'$liveSetting);
  1309.             return $this->createJsonResponse(['fileId' => $file['id'], 'url' => $url'type' => $type]);
  1310.         }
  1311.         $fileId $request->getSession()->get('fileId');
  1312.         list($pictureUrl$naturalSize$scaledSize) = $this->getFileService()->getImgFileMetaInfo($fileId100100);
  1313.         return $this->render('admin/edu-cloud/live/logo-crop-modal.html.twig', [
  1314.             'pictureUrl' => $pictureUrl,
  1315.             'naturalSize' => $naturalSize,
  1316.             'scaledSize' => $scaledSize,
  1317.             'type' => $type,
  1318.         ]);
  1319.     }
  1320.     public function consultSettingAction(Request $request)
  1321.     {
  1322.         if (!$this->isVisibleCloud()) {
  1323.             return $this->redirect($this->generateUrl('admin_my_cloud_overview'));
  1324.         }
  1325.         try {
  1326.             $account $this->getConsultService()->getAccount();
  1327.             $jsResource $this->getConsultService()->getJsResource();
  1328.         } catch (\RuntimeException $e) {
  1329.             return $this->render('admin/edu-cloud/consult/consult-error.html.twig', []);
  1330.         }
  1331.         $cloudConsult $this->getConsultService()->buildCloudConsult($account$jsResource);
  1332.         if (isset($cloudConsult['error'])) {
  1333.             $this->setFlashMessage('danger'$cloudConsult['error']);
  1334.         }
  1335.         unset($cloudConsult['error']);
  1336.         if (== $cloudConsult['cloud_consult_is_buy']) {
  1337.             $this->getSettingService()->set('cloud_consult'$cloudConsult);
  1338.             return $this->renderConsultWithoutEnable($cloudConsult);
  1339.         }
  1340.         if ($request->isMethod('POST')) {
  1341.             $data $request->request->all();
  1342.             $cloudConsult['cloud_consult_setting_enabled'] = $data['cloud_consult_setting_enabled'];
  1343.             $this->getSettingService()->set('cloud_consult'$cloudConsult);
  1344.             $this->setFlashMessage('success''site.save.success');
  1345.         }
  1346.         if (== $cloudConsult['cloud_consult_setting_enabled']) {
  1347.             return $this->renderConsultWithoutEnable($cloudConsult);
  1348.         }
  1349.         return $this->render('admin/edu-cloud/consult/setting.html.twig', [
  1350.             'cloud_consult' => $cloudConsult,
  1351.         ]);
  1352.     }
  1353.     public function getAdAction()
  1354.     {
  1355.         $network $this->getSettingService()->get('developer');
  1356.         if (!isset($network['without_network']) || !$network['without_network']) {
  1357.             $api CloudAPIFactory::create('root');
  1358.             $result $api->get('/edusoho-ad');
  1359.         } else {
  1360.             $result null;
  1361.         }
  1362.         return $this->createJsonResponse($result);
  1363.     }
  1364.     private function renderConsultWithoutEnable($cloudConsult)
  1365.     {
  1366.         return $this->render('admin/edu-cloud/consult/without-enable.html.twig', [
  1367.             'cloud_consult' => $cloudConsult,
  1368.         ]);
  1369.     }
  1370.     protected function setCloudLiveLogo($provider$client)
  1371.     {
  1372.         $setting $this->getSettingService()->get('live-course', []);
  1373.         $isSetLogo = !empty($setting['webLogoPath']) || !empty($setting['appLogoPath']) || !empty($setting['logoUrl']);
  1374.         if ('talkFun' == $provider && $isSetLogo) {
  1375.             $logoData = [
  1376.                 'logoPcUrl' => empty($setting['webLogoPath']) ? '' $setting['webLogoPath'],
  1377.                 'logoClientUrl' => empty($setting['appLogoPath']) ? '' $setting['appLogoPath'],
  1378.                 'logoGotoUrl' => empty($setting['logoUrl']) ? 'http://www.talk-fun.com' $setting['logoUrl'],
  1379.             ];
  1380.             $result $client->setLiveLogo($logoData);
  1381.             if (isset($result['error'])) {
  1382.                 return $this->createMessageResponse('error''设置直播logo出错');
  1383.             }
  1384.         }
  1385.         return true;
  1386.     }
  1387.     protected function deleteCloudMP4Files()
  1388.     {
  1389.         $user $this->getUser();
  1390.         $callback $this->get('request')->getSchemeAndHttpHost().$this->generateUrl('callback', ['type' => 'cloudFile''ac' => 'files.notify']);
  1391.         $this->getCloudFileService()->deleteCloudMP4Files($user['id'], $callback);
  1392.         return true;
  1393.     }
  1394.     protected function refreshCloudSchoolSetting(array $schoolInfo)
  1395.     {
  1396.         PermissionBuilder::instance()->refreshCloudSchoolSetting($schoolInfo);
  1397.         $schoolUserLimitNum PermissionBuilder::instance()->buildSchoolUserLimitNum();
  1398.         PermissionBuilder::instance()->refreshSchoolUserLimitNumSetting($schoolUserLimitNum);
  1399.     }
  1400.     protected function getConsultService()
  1401.     {
  1402.         return $this->createService('EduCloud:MicroyanConsultService');
  1403.     }
  1404.     protected function getFileService()
  1405.     {
  1406.         return $this->createService('Content:FileService');
  1407.     }
  1408.     protected function getCloudFileService()
  1409.     {
  1410.         return $this->createService('CloudFile:CloudFileService');
  1411.     }
  1412.     /**
  1413.      * @return LmsService
  1414.      */
  1415.     protected function getLmsService()
  1416.     {
  1417.         return $this->createService('Lms:LmsService');
  1418.     }
  1419.     /**
  1420.      * @return SmsService
  1421.      */
  1422.     protected function getSmsService()
  1423.     {
  1424.         return $this->getBiz()->service('Sms:SmsService');
  1425.     }
  1426. }