public function get_m3u_data()
 {
     if (!$this->isAjax || $this->method != 'POST') {
         $this->app->abort(404, 'Page not found...');
     }
     if ($no_auth = $this->checkAuth()) {
         return $no_auth;
     }
     $data = array();
     $data['action'] = 'loadM3UData';
     $data['data'] = array('channels' => array(), 'last_channel_number' => 0, 'free_number_exists' => 1);
     $error = $this->setLocalization('Upload failed');
     $storage = new \Upload\Storage\FileSystem('/tmp', TRUE);
     $file = new \Upload\File('qqfile', $storage);
     try {
         // Success!
         $file->upload();
         $obj = new M3uParser\M3uParser();
         $m3u_data = $obj->parseFile($file->getPath() . '/' . $file->getNameWithExtension());
         @unlink($file->getPath() . '/' . $file->getNameWithExtension());
         $data['data']['last_channel_number'] = (int) $this->db->getLastChannelNumber();
         if ($data['data']['last_channel_number'] + count($m3u_data) > 9999) {
             $data['data']['free_number_exists'] = (int) ($this->db->getAllChannels(array(), 'COUNT') + count($m3u_data) <= 9999);
         }
         foreach ($m3u_data as $entry) {
             $name = trim($entry->getName());
             if (!mb_check_encoding($name, 'UTF-8')) {
                 $name = mb_convert_encoding($name, 'UTF-8', array('CP1251'));
             }
             $data['data']['channels'][] = array('name' => $name, 'cmd' => trim($entry->getPath()));
         }
         $error = '';
     } catch (\Exception $e) {
         // Fail!
         $data['msg'] = $error = $file->getErrors();
     }
     $response = $this->generateAjaxResponse($data, $error);
     $json_string = json_encode($response);
     if (json_last_error() !== JSON_ERROR_NONE) {
         $error = $this->setLocalization('Error m3u parse. Check the file encoding. Required UTF-8 encoding.');
         $json_string = json_encode(array('msg' => $error, 'error' => $error));
     }
     return new Response($json_string, empty($error) ? 200 : 500);
 }
示例#2
0
<?php

include 'vendor/autoload.php';
use M3uParser\M3uParser;
$obj = new M3uParser();
$files = glob('m3u/*.m3u');
$a = [];
foreach ($files as $file) {
    $data = $obj->parseFile($file);
    foreach ($data as $entry) {
        $suffix = '';
        $normalizedChannelName = normalizeChannelName($entry->getName());
        $normalizedChannelKey = normalizeChannelKey($normalizedChannelName);
        // check if URL is valid
        if (!validUrl($entry->getPath())) {
            echo "'{$normalizedChannelName}' does not have a valid URL; skipping." . PHP_EOL;
            continue;
        }
        // check for duplicate channel names
        if (isset($a[$normalizedChannelKey])) {
            if ($a[$normalizedChannelKey]['path'] == $entry->getPath()) {
                echo "Skipping duplicate entry for '{$normalizedChannelName}'." . PHP_EOL;
                continue;
            }
            // distinguish them by URL hash
            $suffix = ' | ' . sha1($entry->getPath());
        }
        $a[$normalizedChannelKey . $suffix] = ['name' => $normalizedChannelName . $suffix, 'path' => $entry->getPath()];
    }
}
ksort($a);