|
| 1 | + |
| 2 | +using System; |
| 3 | +using System.Collections; |
| 4 | +using System.Collections.Generic; |
| 5 | +using System.Diagnostics; |
| 6 | +using System.IO; |
| 7 | +using System.Text; |
| 8 | +using Pchp.Core; |
| 9 | +using Pchp.Core.Text; |
| 10 | +using Pchp.Core.Utilities; |
| 11 | +using Pchp.Library.Resources; |
| 12 | +using static Pchp.Core.PhpExtensionAttribute; |
| 13 | + |
| 14 | +namespace Pchp.Library |
| 15 | +{ |
| 16 | + [PhpExtension("wjw")] |
| 17 | + public static class WjwEx |
| 18 | + { |
| 19 | + static WjwEx() |
| 20 | + { |
| 21 | + try |
| 22 | + { |
| 23 | + Encoding.GetEncoding("GBK"); |
| 24 | + } |
| 25 | + catch (Exception) |
| 26 | + { |
| 27 | + Encoding.RegisterProvider(CodePagesEncodingProvider.Instance); |
| 28 | + } |
| 29 | + } |
| 30 | + public static PhpValue wjw_ex_test(Context ctx) |
| 31 | + { |
| 32 | + string str = "WjwEx.cs 源码扩展PhpExtension(\"wjw\")测试: \n增加一个php函数: wjw_ex_test\n"; |
| 33 | + return (PhpValue)str; |
| 34 | + } |
| 35 | + public static PhpValue wjw_sum_test(Context ctx, PhpValue a, PhpValue b) |
| 36 | + { |
| 37 | + int c = (int)a + (int)b; |
| 38 | + return (PhpValue)c; |
| 39 | + } |
| 40 | + /// <summary> |
| 41 | + /// 数学哈希 |
| 42 | + /// </summary> |
| 43 | + /// <param name="ctx"></param> |
| 44 | + /// <param name="str">字串</param> |
| 45 | + /// <returns>数字</returns> |
| 46 | + public static PhpString wjw_hash33(Context ctx, PhpString str) |
| 47 | + { |
| 48 | + byte[] bytes = str.ToBytes(ctx); ; |
| 49 | + int hash = 5381; |
| 50 | + for (int i = 0; i < bytes.Length; i++) |
| 51 | + { |
| 52 | + int c = bytes[i]; |
| 53 | + if (c > 127) c = bytes[i] - 256; |
| 54 | + //WjwPhp.print(bytes[i].ToString()); |
| 55 | + hash += (hash << 5) + c; |
| 56 | + //WjwPhp.print(hash); |
| 57 | + } |
| 58 | + hash &= 0x7fffffff; |
| 59 | + |
| 60 | + return (PhpString)hash.ToString(); |
| 61 | + } |
| 62 | + /// <summary> |
| 63 | + /// 加密 |
| 64 | + /// </summary> |
| 65 | + /// <param name="ctx"></param> |
| 66 | + /// <param name="hdata">源</param> |
| 67 | + /// <param name="hkey">密钥</param> |
| 68 | + /// <returns></returns> |
| 69 | + public static PhpString wjw_crypt(Context ctx, PhpString hdata, PhpString hkey) |
| 70 | + { |
| 71 | + byte[] data = hdata.ToBytes(ctx); |
| 72 | + byte[] key = hkey.ToBytes(ctx); |
| 73 | + |
| 74 | + //if (key == null) key = Encoding.ASCII.GetBytes("pass"); |
| 75 | + int magicnb = 4; |
| 76 | + Int32 i; |
| 77 | + byte chop; |
| 78 | + byte a; |
| 79 | + byte b; |
| 80 | + Int32 paslen = key.Length; |
| 81 | + Int32 datlen = data.Length; |
| 82 | + |
| 83 | + magicnb %= 8; |
| 84 | + for (i = 0; i < datlen; i++) |
| 85 | + { |
| 86 | + chop = key[(datlen - i) % paslen]; |
| 87 | + //data[i] = (chop >> (i % magicnb)) ^ (~(data[i])); |
| 88 | + a = (byte)(chop >> (i % magicnb)); |
| 89 | + b = (byte)(~(data[i])); |
| 90 | + data[i] = (byte)(a ^ b); |
| 91 | + |
| 92 | + } |
| 93 | + return PhpString.ToPhpString(data); |
| 94 | + } |
| 95 | + /// <summary> |
| 96 | + /// 将数组转为csv格式串 |
| 97 | + /// </summary> |
| 98 | + /// <param name="ctx"></param> |
| 99 | + /// <param name="fieldst">数组</param> |
| 100 | + /// <param name="part">分隔符</param> |
| 101 | + /// <param name="quote">包围符</param> |
| 102 | + /// <returns>csv格式串</returns> |
| 103 | + public static PhpString wjw_putcsv(Context ctx, PhpArray fieldst, char part = ',', char quote = '"') |
| 104 | + { |
| 105 | + IEnumerable list = fieldst as IEnumerable; |
| 106 | + string csvStr = ""; |
| 107 | + string fh = ""; |
| 108 | + foreach (var str in list) |
| 109 | + { |
| 110 | + string item = str.ToString(); |
| 111 | + if (item.IndexOf(part) >= 0 || item.IndexOf(quote) >= 0) |
| 112 | + { |
| 113 | + item = item.Replace(quote.ToString(), quote.ToString() + quote.ToString()); |
| 114 | + item = quote.ToString() + item + quote.ToString(); |
| 115 | + } |
| 116 | + csvStr += fh + item; |
| 117 | + fh = part.ToString(); |
| 118 | + } |
| 119 | + return (PhpString)csvStr; |
| 120 | + } |
| 121 | + /// <summary> |
| 122 | + /// 转为GBK串 |
| 123 | + /// </summary> |
| 124 | + /// <param name="ctx"></param> |
| 125 | + /// <param name="strUtf8">UTF-8串</param> |
| 126 | + /// <returns>GBK串</returns> |
| 127 | + public static PhpString to_gbk(Context ctx, PhpString strUtf8) |
| 128 | + { |
| 129 | + byte[] bs = Encoding.GetEncoding("UTF-8").GetBytes(strUtf8.ToString()); |
| 130 | + bs = Encoding.Convert(Encoding.GetEncoding("UTF-8"), Encoding.GetEncoding("GBK"), bs); |
| 131 | + return (PhpString)Encoding.GetEncoding("GBK").GetString(bs); |
| 132 | + |
| 133 | + } |
| 134 | + /// <summary> |
| 135 | + /// 转为UTF-8串 |
| 136 | + /// </summary> |
| 137 | + /// <param name="strGbk">GBK串</param> |
| 138 | + /// <returns>UTF-8串</returns> |
| 139 | + public static PhpString to_utf8(Context ctx, PhpString strGbk) |
| 140 | + { |
| 141 | + byte[] bs = Encoding.GetEncoding("GBK").GetBytes(strGbk.ToString()); |
| 142 | + bs = Encoding.Convert(Encoding.GetEncoding("GBK"), Encoding.GetEncoding("UTF-8"), bs); |
| 143 | + return (PhpString)Encoding.GetEncoding("UTF-8").GetString(bs); |
| 144 | + |
| 145 | + } |
| 146 | + /// <summary> |
| 147 | + /// 判断操作系统为linux |
| 148 | + /// </summary> |
| 149 | + /// <returns></returns> |
| 150 | + public static bool is_linux() |
| 151 | + { |
| 152 | + return CurrentPlatform.IsLinux; |
| 153 | + } |
| 154 | + |
| 155 | + /// <summary> |
| 156 | + /// 初始化配置 |
| 157 | + /// </summary> |
| 158 | + /// <param name="ctx"></param> |
| 159 | + public static void init_config(Context ctx) |
| 160 | + { |
| 161 | + //声明标准规范的脚本加载 |
| 162 | + Spl.Autoload.spl_autoload_register(ctx); |
| 163 | + |
| 164 | + //声明系统文件存储设备的路径 |
| 165 | + if (is_linux()) |
| 166 | + { |
| 167 | + Constants.define(ctx, "DEV_PATH", ""); |
| 168 | + |
| 169 | + } |
| 170 | + else |
| 171 | + { |
| 172 | + Constants.define(ctx, "DEV_PATH", "d:/wjwlibs_cs"); |
| 173 | + Constants.define(ctx, "ROOTPATH", "d:/wjwlibs_cs"); |
| 174 | + } |
| 175 | + Constants.define(ctx, "WJW_PHPCS_VERSION", "1.0.0"); |
| 176 | + } |
| 177 | + /// <summary> |
| 178 | + /// 从控制台读入一行字串 |
| 179 | + /// </summary> |
| 180 | + /// <returns></returns> |
| 181 | + public static PhpString read_line() |
| 182 | + { |
| 183 | + return (PhpString)Console.ReadLine(); |
| 184 | + } |
| 185 | + } |
| 186 | +} |
0 commit comments