<?php

/*
eD2k Link Selector php class 简体中文
http://emulefans.com/php-class-ed2k-link-selector/
v1.1.7
by tomchen1989
GPL v2

eD2k Link Selector php class可以将eD2k链接转换为一个显示链接并带有过滤选择器的表格。与ed2kls.js、ed2kls.css与ZeroClipboard.swf一同使用。使用方法见readme.txt或http://emulefans.com/php-class-ed2k-link-selector/。

如想在WordPress blog上使用，请见：
http://emulefans.com/wordpress-ed2k-link-selector/
*/

/*
Copyright 2010 tomchen1989/emulefans.com (email: admin@emulefans.com)

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/

define('ED2KLS_URL', 'http://localhost/ed2k-link-selector-php');

class elsConvert {

		function randomkeys($length){
			$pattern='1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLOMNOPQRSTUVWXYZ';
			for ($i=0; $i<$length; $i++){
				$key .= $pattern{mt_rand(0,62)};
			}
			return $key;
		}

		function formatSize($val) {
			$sep = 100;
			$unit = '字节';
			if ($val >= 1099511627776) {
				$val = round($val / (1099511627776 / $sep)) / $sep;
				$unit = 'TB';
			} else if ($val >= 1073741824) {
				$val = round($val / (1073741824 / $sep)) / $sep;
				$unit = 'GB';
			} else if ($val >= 1048576) {
				$val = round($val / (1048576 / $sep)) / $sep;
				$unit = 'MB';
			} else if ($val >= 1024) {
				$val = round($val / (1024 / $sep)) / $sep;
				$unit = 'KB';
			}
			return $val . $unit;
		}

		function convert($content, $head = "eD2k链接", $stat = "http://ed2k.shortypower.org/?hash=", $name = "auto", $size = "auto", $collection = "true", $width = "100%", $fontsize = "13px", $buttonstyle = "0", $no = NULL) {

			if ($no === NULL) {
				$no = $this->randomkeys(8);
			}
			$sizetot = 0;
			$num = 0;
			$extarray = array();
			$newcontent = '';
			if ( strtolower($collection) != "false" ) {
				$newcontent .= '
<form action="' . constant('ED2KLS_URL') . '/emcl.php" method="POST" id="el-s-form-' . $no . '" onsubmit="return ed2kls.emclChk(\'' . $no . '\');">';
			}
			$newcontent .= '
<table class="el-s';
			if ( $buttonstyle == 1 ) {
				$newcontent .= ' el-s-buttonimg';
			} else if ( $buttonstyle == 2 ) {
				$newcontent .= ' el-s-buttonimg el-s-buttonimg2';
			}
			$newcontent .= '" id="el-s-' . $no . '" border="0" cellpadding="0" cellspacing="0" style="width:' . $width . ';font-size:' . $fontsize . ';">
	<thead class="el-s-thead">
		<tr><td colspan="2">
			<div class="el-s-titlebtn el-s-toright">
				<a id="el-s-help-' . $no . '" class="el-s-pseubtn el-s-hlp el-s-toright" title="' . '帮助' . '" onclick="ed2kls.help(\'' . $no . '\',0)">[?]</a><a id="el-s-exd-' . $no . '" class="el-s-pseubtn el-s-exd el-s-toright" title="' . '隐藏' . '" onclick="ed2kls.close(\'' . $no . '\')">[-]</a>
			</div>
			<strong>' . $head . '</strong><noscript><br /><span style="color:red!important;">' . '请开启浏览器的JavaScript功能以访问该页面。' . '</span></noscript>
		</td></tr>
	</thead>
	<tfoot>
		<tr class="el-s-infotr"><td colspan="2">
			<div id="el-s-info-' . $no . '" class="el-s-info" style="display: none;">
				<a id="el-s-info-close-' . $no . '" class="el-s-pseubtn el-s-info-close el-s-toright" title="' . '关闭帮助信息' . '" onclick="ed2kls.closeinfo(\'' . $no . '\')">[×]</a>
				<div id="el-s-info-desc-' . $no . '" class="el-s-info-desc">' . '帮助信息：' . '</div>
				<div id="el-s-info-content-' . $no . '" class="el-s-info-content"></div>
			</div>
		</td></tr>
		<tr class="el-s-bottom"><td colspan="2">
			<a id="el-s-help2-' . $no . '" title="' . '帮助' . '" onclick="ed2kls.help(\'' . $no . '\',0)">' . '帮助' . '</a>
			<span class="el-s-sep">|</span>
			<a href="http://www.emule-project.net/" target="_blank" title="' . 'eMule官方网站' . '">' . 'eMule官方' . '</a>
			<span class="el-s-sep">|</span>
			<a href="http://emulefans.com/" target="_blank" title="' . 'eMuleFans.com [电骡爱好者中文Blog]' . '">' . 'eMule Fans 电骡爱好者' . '</a>
			<span class="el-s-sep">|</span>
			<a href="http://www.emule-mods.de/" target="_blank" title="' . 'eMule-Mods.de [eMule Mods站点]' . '">eMule-Mods.de</a>
			<span class="el-s-sep">|</span>
			<a href="http://emulefans.com/php-ed2k-link-selector/" target="_blank" title="' . 'eD2k Link Selector（eD2k链接选择器）php类主页' . '">' . 'php类' . '</a>
		</td></tr>
	</tfoot>
	<tbody id="el-s-tb-' . $no . '">
';

			$content = preg_replace (
			"/(?<!ed2k=)(?<!ed2k=[\"\'])(?<!href=)(?<!href=[\"\'])ed2k:\/\/\|file\|.+?\|\/(?!\|)/i",
			"\n\\0\n",
			$content
			);

			preg_match_all (
			"/^.+$/m",
			$content,
			$lines
			);

			foreach ($lines[0] as $myline) {
				preg_match (
				"/(?<!ed2k=)(?<!ed2k=[\"\'])(?<!href=)(?<!href=[\"\'])ed2k:\/\/\|(file)\|(.+?)\|\/(?!\|)/i",
				$myline,
				$matches
				);
				if (count($matches) != 0) {
					$num += 1;
					if ($num%2) {
						$odd = 1;
					} else {
						$odd = 2;
					}
					$url = $matches[0];
					//$type = $matches[1];
					$pieces = explode("|", $matches[2]);
					$myname = urldecode($pieces[0]);
					if (strrchr($myname, '.')) {
						$ext = strtolower(trim(substr(strrchr($myname, '.'), 1, 15)));
						array_push($extarray, $ext);
					}
					$mysize = $pieces[1];
					$sizetot += $mysize;
					$mysize = $this->formatSize($mysize);
					$hash = $pieces[2];
					$newcontent .= '
<tr class="el-s-tr' . $odd . '">
	<td class="el-s-left">
		<input type="checkbox" class="el-s-chkbx el-s-chkbx-ed2k" name="el-s-chkbx-' . $no . '[]" id="el-s-chkbx-' . $no . '-' . $num . '" value="' . $url . '" onclick="ed2kls.checkIt(\'' . $no . '\',event);" checked="checked" /><a class="el-s-dl" href="' . $url . '" ed2k="' . $url . '">' . $myname . '</a>';
					if (strtolower($stat) != "false") {
						$newcontent .= '
		<a class="el-s-viewsrc" href="' . $stat . $hash . '" target="_blank">' . '查源' . '</a>';
					}
					$newcontent .= '
	</td>
	<td class="el-s-right">' . $mysize . '</td>
</tr>';
				} else {
					$myline = preg_replace (
					"/<p>|<\/p>|<br\s\/>|<br\/>|<br>/i",
					"",
					$myline
					);
					$myline = trim($myline);
					if ($myline !== "") {
						$newcontent .= '
<tr class="el-s-desctr">
	<td colspan="2">
'. $myline .'
	</td>
</tr>';
					}
				}

			}

			$sizetot = $this->formatSize($sizetot);

			$newcontent .= '
		<tr class="el-s-selecttr">
			<td class="el-s-left">
				<span class="el-s-area"><input type="checkbox" class="el-s-chkbx el-s-chkall" id="el-s-chkall-' . $no . '" onclick="ed2kls.checkAll(\'' . $no . '\',this.checked)" checked="checked" /><label class="el-s-chkall" for="el-s-chkall-' . $no . '">' . '全选' . '</label></span>';

			if ( (strtolower($name) != 'false' && $num >= 2) || strtolower($name) == 'true') {
				$newcontent .= '
				<span class="el-s-area el-s-area-label"><label class="el-s-namefilter" for="el-s-namefilter-' . $no . '">' . '文件名选择' . '</label><a id="el-s-namefilterhelp-' . $no . '" class="el-s-pseubtn el-s-hlp" title="' . '帮助' . '" onclick="ed2kls.help(\'' . $no . '\',1)">[?]</a>:<input type="text" class="el-s-txt el-s-namefilter" id="el-s-namefilter-' . $no . '" onkeyup="ed2kls.filter(\'' . $no . '\')" />';

				$extarray = array_unique($extarray);

				foreach ($extarray as $myext) {
					$newcontent .= '
				<input type="checkbox" value="' . $myext . '" name="el-s-chktype-' . $no . '[]" class="el-s-chkbx el-s-chktype" id="el-s-chktype-' . $myext . '-' . $no . '" onclick="ed2kls.typeFilter(\'' . $no . '\',this.value,this.checked)" /><label class="el-s-filter" for="el-s-chktype-' . $myext . '-' . $no . '">' . strtoupper($myext) . '</label>';
				}

				$newcontent .= '</span>';
			}

			if ( (strtolower($size) != 'false' && $num >= 2) || strtolower($size) == 'true') {
				$newcontent .= '
				<span class="el-s-area el-s-area-label"><label class="el-s-sizefilter">' . '大小选择' . '</label><a id="el-s-sizefilterhelp-' . $no . '" class="el-s-pseubtn el-s-hlp" title="' . '帮助' . '" onclick="ed2kls.help(\'' . $no . '\',2)">[?]</a>:<select id="el-s-sizesymbol-' . $no . '-1" class="el-s-sel" onchange="ed2kls.filter(\'' . $no . '\')">
					<option selected="selected" value="1">&gt;</option>
					<option value="2">&lt;</option>
				</select><input type="text" class="el-s-txt el-s-sizefilter" id="el-s-sizefilter-' . $no . '-1" onkeyup="ed2kls.filter(\'' . $no . '\')" /><select id="el-s-sizeunit-' . $no . '-1" class="el-s-sel" onchange="ed2kls.filter(\'' . $no . '\')">
					<option selected="selected" value="1099511627776">' . 'TB' . '</option>
					<option value="1073741824">' . 'GB' . '</option>
					<option value="1048576">' . 'MB' . '</option>
					<option value="1024">' . 'KB' . '</option>
					<option value="1">' . '字节' . '</option>
				</select>,<select id="el-s-sizesymbol-' . $no . '-2" class="el-s-sel" onchange="ed2kls.filter(\'' . $no . '\')">
					<option selected="selected" value="1">&gt;</option>
					<option value="2">&lt;</option>
				</select><input type="text" class="el-s-txt el-s-sizefilter" id="el-s-sizefilter-' . $no . '-2" onkeyup="ed2kls.filter(\'' . $no . '\')" /><select id="el-s-sizeunit-' . $no . '-2" class="el-s-sel" onchange="ed2kls.filter(\'' . $no . '\')">
					<option selected="selected" value="1099511627776">' . 'TB' . '</option>
					<option value="1073741824">' . 'GB' . '</option>
					<option value="1048576">' . 'MB' . '</option>
					<option value="1024">' . 'KB' . '</option>
					<option value="1">' . '字节' . '</option>
				</select></span>';
			}

			$newcontent .= '
			</td>
			<td rowspan="2" class="el-s-right"><span id="el-s-totsize-' . $no . '">' . $sizetot .'</span><br />(<span id="el-s-totnum-' . $no . '">' . $num . '</span>' . '文件' . ')</td>
		</tr>
		<tr class="el-s-buttontr">
			<td class="el-s-left">
				<input type="button" id="el-s-download-' . $no . '" class="el-s-button el-s-download" onclick="ed2kls.download(\'' . $no . '\')" title="' . '下载选中文件' . '" value="' . '下载选中文件' . '" />
				<input type="button" id="el-s-copylinks-' . $no . '" class="el-s-button el-s-copylinks" onclick="ed2kls.cb.iecopy(2,\'' . $no . '\')" title="' . '复制选中链接' . '" value="' . '复制选中链接' . '" />
				<input type="button" id="el-s-copynames-' . $no . '" class="el-s-button el-s-copynames" onclick="ed2kls.cb.iecopy(1,\'' . $no . '\')" title="' . '复制选中名称' . '" value="' . '复制选中名称' . '" />';
			if ( strtolower($collection) != "false" ) {
				$newcontent .= '
				<input type="hidden" value="' . $no . '" name="el-s-no"><input type="submit" id="el-s-submit-' . $no . '" class="el-s-button el-s-emcl" title="' . 'eMule收藏集' . '" value="' . 'eMule收藏集' . '" />';
			}
			$newcontent .= '
				<span class="el-s-copied" id="el-s-copied-' . $no . '" style="display:none;"><span class="el-s-yes">√</span>' . '已复制' . '</span>
			</td>
		</tr>
	</tbody>
</table>';
			if ( strtolower($collection) != "false" ) {
				$newcontent .= '
</form>';
			}
			return $newcontent;

		}

		function showConvert() {
			$arg_list = func_get_args();
			echo call_user_func_array(array($this, "convert"), $arg_list);
		}

		function addHead() {
			echo '
<link rel="stylesheet" type="text/css" href="' . constant('ED2KLS_URL') . '/ed2kls.css" /><!-- eD2k Link Selector CSS -->
';
		}

		function addFooter() {
			echo '
<!-- START of eD2k Link Selector -->
<div style="display:none;">
<div id="el-s-info-content-str-0" class="el-s-info-content-str">
' . '您可以使用<a href="http://www.emule-project.net/home/perl/general.cgi?l=42&rm=download">eMule</a>或eMule Mod（参见<a href="http://emulefans.com/news/emule/">eMuleFans.com的Mod页</a>、<a href="http://www.emule-mods.de/?mods=start">emule-mods.de的Mod页</a>）（Windows）、<a href="http://www.amule.org/">aMule</a>（Win、Linux、Mac）等软件下载eD2k链接。可以参考这里的<a href="http://emulefans.com/ed2k-links-handle/">修复、关联eD2k协议链接方法</a>。<br />eMule收藏集（.emulecollection）文件是您选中的所有链接的列表文件。eMule可以直接下载它们。<br />按住SHIFT键选择可以选中多个选择框。<br />可用文件名和大小选择器来选择文件。<br />查看<a href="http://emulefans.com/php-ed2k-link-selector/">eD2k Link Selector php类主页</a>可以下载此php类或联系作者。<br />查看<a href="http://emulefans.com/wordpress-ed2k-link-selector/">eD2k Link Selector WordPress 插件主页</a>可以下载WordPress插件。' . '
</div>
<div id="el-s-info-content-str-1" class="el-s-info-content-str">
' . '文件名选择器帮您根据文件名称或后缀来选择文件。不分大小写。<br />符号使用：<br />和：空格（<code> </code>）、<code>+</code>；<br />不包含：<code>-</code>；<br />或：<code>|</code>；<br />转义：一对英文引号（<code>""</code>）；<br />匹配开头：<code>^</code>；<br />匹配结尾：<code>$</code>。<br />例如：<br />选中所有名称中包含有“eMule”或“0.49c”字眼，但不包含有“exe”字眼的：<code>emule|0.49c -exe</code>；<br />选中所有名称的开头是“eMule”，结尾是“0.49c”的：<code>^emule 0.49c$</code>；<br />选中所有名称中带有“eMule 0.49c”的（必须是“eMule 0.49c”，中间没有别的字符，不能是“eMule fake 0.49c”），需要转义：<code>"emule 0.49c"</code>。' . '
</div>
<div id="el-s-info-content-str-2" class="el-s-info-content-str">
' . '大小选择器帮您根据文件大小选择文件。' . '
</div>
</div>
<script type="text/javascript">//<![CDATA[
var ed2klsPath="' . constant('ED2KLS_URL') . '";
var ed2klsVar = {};
ed2klsVar.retry = "' . '载入未完成。请重试。' . '";
ed2klsVar.shk = "' . '隐藏' . '";
ed2klsVar.exd = "' . '显示' . '";
ed2klsVar.bytes = "' . '字节' . '";
ed2klsVar.tb = "' . 'TB' . '";
ed2klsVar.gb = "' . 'GB' . '";
ed2klsVar.mb = "' . 'MB' . '";
ed2klsVar.kb = "' . 'KB' . '";
//]]></script>
<script type="text/javascript" src="' . constant('ED2KLS_URL') . '/ed2kls.js"></script>
<!-- END of eD2k Link Selector -->
';
		}

}

?>