invoice.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646
  1. <?php
  2. Doo::loadCore ( 'db/DooModel' );
  3. /**
  4. * 发票审批相关信息及其操作业务逻辑
  5. * @author CP.
  6. * @version 1.0
  7. * @namespace invoice
  8. * @package invoiceModel
  9. */
  10. class invoice extends DooModel {
  11. private $INVOICEKEY = "APPROVAL";
  12. /**
  13. *
  14. * @var integer $iid 发票ID
  15. */
  16. public $iid;
  17. /**
  18. *
  19. * @var integer $status 审批状态
  20. */
  21. public $status;
  22. /**
  23. *
  24. * @var string $invoiceManage 当前审批组人员
  25. */
  26. public $printStatus;
  27. public $postStatus;
  28. public $invoiceManage;
  29. /**
  30. *
  31. * @var string $pendingApprovals 当前需要审批的人
  32. */
  33. public $pendingApprovals;
  34. /**
  35. *
  36. * @var string $processApprovals 已经审批过的人员
  37. */
  38. public $processApprovals;
  39. /**
  40. *
  41. * @var string $invoiceSerial 发票单号
  42. */
  43. public $invoiceSerial;
  44. /**
  45. *
  46. * @var integer $invoicePrice 发票金额
  47. */
  48. public $invoicePrice;
  49. /**
  50. * 管理组人员:发票打印,
  51. * @var unknown
  52. */
  53. public $moldManage;
  54. /**
  55. *
  56. * @var integer $cid 办事处ID
  57. */
  58. public $cid;
  59. /**
  60. *
  61. * @var string $categoryName 办事处名称
  62. */
  63. public $categoryName;
  64. /**
  65. *
  66. * @var integer $sid 提交发票人ID
  67. */
  68. public $sid;
  69. /**
  70. *
  71. * @var string $userName 提交人名字
  72. */
  73. public $userName;
  74. /**
  75. *
  76. * @var string $remark 备注
  77. */
  78. public $remark;
  79. /**
  80. *
  81. * @var string $invoiceElement 发票内容
  82. */
  83. public $invoiceElement;
  84. /**
  85. *
  86. * @var string $invoiceType 发票类型
  87. */
  88. public $invoiceType;
  89. /**
  90. *
  91. * @var string $invoiceTitle 发票抬头
  92. */
  93. public $invoiceTitle;
  94. /**
  95. *
  96. * @var string $invoiceCompany 开票公司
  97. */
  98. public $invoiceCompany;
  99. /**
  100. *
  101. * @var string $invoiceNo 发票号
  102. */
  103. public $invoiceNo;
  104. /**
  105. *
  106. * @var string $TIN 纳税人识别码
  107. */
  108. public $TIN;
  109. /**
  110. *
  111. * @var string $address 注册地址
  112. */
  113. public $address;
  114. /**
  115. *
  116. * @var string $phone 电话
  117. */
  118. public $phone;
  119. /**
  120. *
  121. * @var string $bank 开户银行
  122. */
  123. public $bank;
  124. /**
  125. *
  126. * @var string $bankAccount 银行账户
  127. */
  128. public $bankAccount;
  129. /**
  130. *
  131. * @var integer $doPost 邮寄
  132. */
  133. public $doPost;
  134. /**
  135. *
  136. * @var string $recipients 收件人
  137. */
  138. public $recipients;
  139. /**
  140. *
  141. * @var string $recipientsPhone 收件人电话
  142. */
  143. public $recipientsPhone;
  144. /**
  145. *
  146. * @var string $recipientsAddress 收件地址
  147. */
  148. public $recipientsAddress;
  149. /**
  150. *
  151. * @var string $mailItems 邮寄物品
  152. */
  153. public $mailItems;
  154. public $mailItemsJson;
  155. public $expressCompany;
  156. public $expressNumber;
  157. public $actualItems;
  158. public $poster;
  159. public $untreadReason;
  160. public $untreadPost;
  161. public $untreadCompany;
  162. public $untreadNumber;
  163. public $untreadItems;
  164. public $untreadStatus;
  165. public $irid;
  166. public $bindTime;
  167. /**
  168. *
  169. * @var date $date 提交时间
  170. */
  171. public $date;
  172. public $isDelete;
  173. /**
  174. *
  175. * @var date $updateTime 更新时间
  176. */
  177. public $updateTime;
  178. /**
  179. *
  180. * @var date $printTime 打印时间
  181. */
  182. public $printTime;
  183. public $postTime;
  184. public $_table = 'CLD_invoice';
  185. public $_primarykey = 'iid';
  186. public $_fields = array (
  187. 'iid',
  188. 'invoiceManage',
  189. 'pendingApprovals',
  190. 'processApprovals',
  191. 'invoiceSerial',
  192. 'status',
  193. 'printStatus',
  194. 'postStatus',
  195. 'untreadStatus',
  196. 'invoicePrice',
  197. 'moldManage',
  198. 'cid',
  199. 'categoryName',
  200. 'irid',
  201. 'remark',
  202. 'invoiceElement',
  203. 'invoiceType',
  204. 'invoiceTitle',
  205. 'invoiceCompany',
  206. 'invoiceNo',
  207. 'TIN',
  208. 'address',
  209. 'phone',
  210. 'bank',
  211. 'bankAccount',
  212. 'doPost',
  213. 'recipients',
  214. 'recipientsPhone',
  215. 'recipientsAddress',
  216. 'mailItems',
  217. 'mailItemsJson',
  218. 'expressCompany',
  219. 'expressNumber',
  220. 'actualItems',
  221. 'untreadReason',
  222. 'untreadPost',
  223. 'untreadCompany',
  224. 'untreadNumber',
  225. 'untreadItems',
  226. 'poster',
  227. 'sid',
  228. 'userName',
  229. 'date',
  230. 'isDelete',
  231. 'updateTime',
  232. 'printTime',
  233. 'postTime',
  234. 'bindTime'
  235. );
  236. /**
  237. * 根据发票ID获取一条发票数据
  238. * @param number $iid 发票ID
  239. * @param number $select 需要获取的字段,为空获取全部数据
  240. * @return array|array() 返回发票数据
  241. */
  242. public function getInvoiceByIid($iid = 0, $select = "") {
  243. if (! is_numeric ( $iid ))
  244. $iid = $this->authcode ( $iid );
  245. $condition = array (
  246. 'where' => "iid=" . $iid,
  247. 'asArray' => TRUE
  248. );
  249. if (! empty ( $select ))
  250. $condition += array (
  251. 'select' => $select
  252. );
  253. $Detail = array ();
  254. if (is_numeric ( $iid ) && ! empty ( $iid ))
  255. $Detail = $this->getOne ( $condition );
  256. if (isset ( $Detail ['expressCompany'] ) && ! empty ( $Detail ['expressCompany'] )) {
  257. $express = explode ( ":", $Detail ['expressCompany'] );
  258. $Detail ['expressCompany'] = $express [0];
  259. $Detail ['expressCom'] = $express [1];
  260. } else {
  261. $Detail ['expressCompany'] = '';
  262. $Detail ['expressCom'] = '';
  263. }
  264. $Detail ['iidKey'] = $this->authcode ( $Detail ['iid'], '' );
  265. Doo::loadModel ( 'L_category' );
  266. $lCategory = new L_category ();
  267. $Detail ['category'] = $lCategory->getCategory ();
  268. return $Detail;
  269. }
  270. public function getInvoiceByIsid($iid = "", $sid = 0) {
  271. if (empty ( $iid ) || empty ( $sid ))
  272. return array ();
  273. Doo::loadClass ( 'XDeode' );
  274. $XDeode = new XDeode ( 5 );
  275. $iid = $XDeode->decode ( $iid );
  276. if (! is_numeric ( $iid ))
  277. return array ();
  278. $detail = $this->getOne ( array (
  279. 'where' => "iid= '" . $iid . "'", // and sid=".$sid,
  280. 'asArray' => TRUE
  281. ) );
  282. return $detail;
  283. }
  284. /**
  285. * 获取单个可以打印的发票数据
  286. * @param number $iid 发票id
  287. * @param number $select 需要获取的字段,为空获取全部数据
  288. * @return array|array() 返回发票数据
  289. */
  290. public function getInvoiceByPrint($iid = 0, $select = "") {
  291. $condition = array (
  292. 'where' => "iid= '" . $iid . "' and status=2",
  293. 'asArray' => TRUE
  294. );
  295. if (! empty ( $select ))
  296. $condition += array (
  297. 'select' => $select
  298. );
  299. $detail = array ();
  300. if (! empty ( $iid ) && is_numeric ( $iid ))
  301. $detail = $this->getOne ( $condition );
  302. return $detail;
  303. }
  304. /**
  305. * 添加一个发票并进入审批状态
  306. * @param array $item 发票相关数据
  307. * @return number 返回发票ID
  308. */
  309. public function addInvoice($item = array()) {
  310. $lid = 0;
  311. if (is_array ( $item ) && ! empty ( $item )) {
  312. foreach ( $item as $key => $value ) {
  313. $this->$key = $value;
  314. }
  315. $lid = $this->insert ();
  316. }
  317. return $lid;
  318. }
  319. /**
  320. * 根据参数字段更新相应字段(主键ID必须传)
  321. * @param array $item 相关需要更新的字段信息
  322. * @return number 返回发票ID
  323. */
  324. public function setInvoiceByCondition($item = array()) {
  325. $lid = 0;
  326. if (is_array ( $item ) && ! empty ( $item )) {
  327. foreach ( $item as $key => $value ) {
  328. $this->$key = $value;
  329. }
  330. $lid = $this->update ();
  331. }
  332. return $lid;
  333. }
  334. /**
  335. * 根据管理组和发票当前状态获得打印发票数据;其中当iid为空时获取所有发票数据,反之获取1条数据
  336. * @param number $sid 管理组人员ID
  337. * @param number $status 状态 为0时获取可以打印数据,为1时获取已经完成打印数据
  338. * @param number $iid 发票ID
  339. * @return array|array() 返回所有发票数据,当iid有值时返回一条数据
  340. */
  341. public function getPrintInvoiceByManage($sid = 0, $status = 0, $iid = 0) {
  342. $list = array ();
  343. if (! empty ( $sid ) && empty ( $iid ))
  344. $list = $this->find ( array (
  345. 'where' => "status=2 and printStatus=" . $status . " and moldManage like '%[\"" . $sid . "\",%'",
  346. 'desc' => 'iid',
  347. 'asArray' => TRUE
  348. ) );
  349. elseif (! empty ( $sid ) && ! empty ( $iid ))
  350. $list = $this->getOne ( array (
  351. 'where' => "status=2 and printStatus=" . $status . " and moldManage like '%[\"" . $sid . "\",%' and iid=" . $iid,
  352. 'asArray' => TRUE
  353. ) );
  354. return $list;
  355. }
  356. /**
  357. * 获得与我相关的发票数据,其中包含 处理中,待处理,最旧入账等数据;当iid有值时获取一条关于sid的发票
  358. * @param number $sid 用户ID
  359. * @param number $iid 发票ID
  360. * @return array|array()
  361. */
  362. public function getMyInvoice($sid = 0, $iid = 0) {
  363. Doo::loadModel ( 'invoiceOperationLog' );
  364. $invoiceOperationLog = new invoiceOperationLog ();
  365. Doo::loadClass ( 'XDeode' );
  366. $XDeode = new XDeode ( 5 );
  367. $list = array ();
  368. if (! empty ( $iid )) {
  369. $list = $this->getOne ( array (
  370. 'where' => " sid=" . $sid . " and iid=" . $iid,
  371. 'asArray' => TRUE
  372. ) );
  373. } elseif (! empty ( $sid ) && empty ( $iid )) {
  374. $list ['pendingInvoice'] = array ();
  375. $list ['handleInvoice'] = array ();
  376. $list ['pendingInvoice'] = $this->find ( array (
  377. 'where' => "((status=1) or (status=2 and printStatus=0) or ( status=2 and printStatus=1 and untreadStatus=1) ) and sid=" . $sid,
  378. 'desc' => 'iid',
  379. 'asArray' => TRUE
  380. ) );
  381. foreach ( $list ['pendingInvoice'] as $key => $value ) {
  382. $list ['pendingInvoice'] [$key] ['OperationLog'] = $invoiceOperationLog->getInvoiceOperationLogByIid ( $value ['iid'], 'desc' );
  383. $list ['pendingInvoice'] [$key] ['iidKeyUrl'] = $XDeode->encode ( $value ['iid'] );
  384. }
  385. $list ['handleInvoice'] = $this->find ( array (
  386. // 需求变更 -去除出票状态的数据 or (status=2 and printStatus=1 and untreadStatus=0 ) or (status=2 and printStatus=1 and untreadStatus=2 )
  387. 'where' => "(status=3 or status=4 or ((status=2 and printStatus=1 and untreadStatus=0 ) or status=2 and printStatus=1 and untreadStatus=2 ) ) and isDelete=0 and sid=" . $sid,
  388. 'desc' => 'iid',
  389. 'asArray' => TRUE
  390. ) );
  391. foreach ( $list ['handleInvoice'] as $key => $value ) {
  392. $list ['handleInvoice'] [$key] ['iidKey'] = $this->authcode ( $value ['iid'], '' );
  393. $list ['handleInvoice'] [$key] ['iidKeyUrl'] = $XDeode->encode ( $value ['iid'] );
  394. $list ['handleInvoice'] [$key] ['OperationLog'] = $invoiceOperationLog->getInvoiceOperationLogByIid ( $value ['iid'], 'desc' );
  395. }
  396. }
  397. return $list;
  398. }
  399. /**
  400. * 获得所有未入账发票
  401. * @param number $invoiceTC 发票title
  402. */
  403. public function getInvoiceByUnAccount($invoiceTC='') {
  404. Doo::loadClass ( 'XDeode' );
  405. $XDeode = new XDeode ( 5 );
  406. $title="";
  407. if (!empty($invoiceTC))
  408. $title=' and (invoiceTitle like "%'.$invoiceTC.'%" or invoiceCompany like "%'.$invoiceTC.'%" )';
  409. $list = $this->find ( array (
  410. 'where' => "status=2 and printStatus=1 and untreadStatus=0 and irid='' and isDelete=0 ".$title,
  411. 'desc' => 'iid',
  412. 'asArray' => TRUE
  413. ));
  414. foreach ( $list as $key => $value ) {
  415. $list [$key] ['iidKeyUrl'] = $XDeode->encode ( $value ['iid'] );
  416. }
  417. return $list;
  418. }
  419. /**
  420. * 获得与我有关可收款的发票,已经出票的发票
  421. * @param number $sid 开票人ID
  422. */
  423. public function getInvoiceByReceivables($sid = 0) {
  424. Doo::loadClass ( 'XDeode' );
  425. $XDeode = new XDeode ( 5 );
  426. $list ['handleInvoice'] = $this->find ( array (
  427. 'where' => "(status=2 and printStatus=1 and untreadStatus=0 ) and isDelete=0 and sid=" . $sid,
  428. 'desc' => 'iid',
  429. 'asArray' => TRUE
  430. ) );
  431. foreach ( $list ['handleInvoice'] as $key => $value ) {
  432. $list ['handleInvoice'] [$key] ['iidKey'] = $this->authcode ( $value ['iid'], '' );
  433. $list ['handleInvoice'] [$key] ['iidKeyUrl'] = $XDeode->encode ( $value ['iid'] );
  434. // $list ['handleInvoice'] [$key] ['OperationLog'] = $invoiceOperationLog->getInvoiceOperationLogByIid ( $value ['iid'], 'desc' );
  435. }
  436. return $list ['handleInvoice'];
  437. }
  438. /**
  439. * 获取需要邮寄的发票
  440. * @param integer $postStatus 0为获取需要邮寄的发票,1为获取已经邮寄的发票
  441. * @return array|array() 返回相关数据
  442. */
  443. public function getPostByInvoice($postStatus = 0) {
  444. Doo::loadClass ( 'XDeode' );
  445. $XDeode = new XDeode ( 5 );
  446. $list = $this->find ( array (
  447. 'where' => "postStatus=" . $postStatus . " and doPost=1 and status=2 and isDelete=0",
  448. 'asArray' => TRUE
  449. ) );
  450. foreach ( $list as $key => $value ) {
  451. $list [$key] ['iidKey'] = $this->authcode ( $value ['iid'], '' );
  452. $list [$key] ['iidKeyK'] = $XDeode->encode ( $value ['iid'] );
  453. $list [$key] ['expressCompany'] = '';
  454. $list [$key] ['expressCom'] = '';
  455. if (! empty ( $value ['expressCompany'] )) {
  456. $express = explode ( ":", $value ['expressCompany'] );
  457. $list [$key] ['expressCompany'] = $express [0];
  458. $list [$key] ['expressCom'] = $express [1];
  459. }
  460. }
  461. return $list;
  462. }
  463. /**
  464. * 根据退票状态获得相关数据,iid有值时只获得一条数据
  465. * @param number $untreadStatus 退票状态
  466. * @param number $iid 发票ID
  467. */
  468. public function getInvoiceByUntreadStatus($untreadStatus = 1, $iid = 0) {
  469. Doo::loadClass ( 'XDeode' );
  470. $XDeode = new XDeode ( 5 );
  471. if (empty ( $iid )) {
  472. $list = $this->find ( array (
  473. 'where' => " printStatus=1 and untreadStatus=" . $untreadStatus . " and status=2 ",
  474. 'asArray' => TRUE
  475. ) );
  476. if ($untreadStatus == 2) {
  477. Doo::loadModel ( 'invoiceOperationLog' );
  478. $invoiceOperationLog = new invoiceOperationLog ();
  479. }
  480. foreach ( $list as $key => $value ) {
  481. $list [$key] ['iidKey'] = $this->authcode ( $value ['iid'], '' );
  482. $list [$key] ['iidKeyK'] = $XDeode->encode ( $value ['iid'] );
  483. $list [$key] ['untreadCompany'] = '';
  484. $list [$key] ['untreadCom'] = '';
  485. if (! empty ( $value ['untreadCompany'] )) {
  486. $express = explode ( ":", $value ['untreadCompany'] );
  487. $list [$key] ['untreadCompany'] = $express [0];
  488. $list [$key] ['untreadCom'] = $express [1];
  489. }
  490. if ($untreadStatus == 2) {
  491. $list [$key] ['OperationLog'] = $invoiceOperationLog->getInvoiceOperationByStatus ( $value ['iid'], 6 );
  492. }
  493. }
  494. } else {
  495. $list = $this->getOne ( array (
  496. 'where' => "iid=" . $iid . " and printStatus=1 and untreadStatus=" . $untreadStatus . " and status=2",
  497. 'asArray' => TRUE
  498. ) );
  499. }
  500. return $list;
  501. }
  502. /**
  503. * 审批中获取相关数据 包括最近的一条操作记录
  504. * @param number $status.
  505. * @param number $limit.
  506. */
  507. function getInvoiceByProcessStatus($limit = 4, $desc = 'desc') {
  508. Doo::loadClass ( 'XDeode' );
  509. $XDeode = new XDeode ( 5 );
  510. $list = $this->find ( array (
  511. 'where' => " (status=1 or status=3 or status=4) and processApprovals!=''",
  512. 'limit' => $limit,
  513. $desc => 'iid',
  514. 'asArray' => TRUE
  515. ) );
  516. Doo::loadModel ( 'invoiceOperationLog' );
  517. $invoiceOperationLog = new invoiceOperationLog ();
  518. foreach ( $list as $key => $value ) {
  519. $list [$key] ['iidKeyK'] = $XDeode->encode ( $value ['iid'] );
  520. $list [$key] ['operationLog'] = $invoiceOperationLog->getInvoiceOperationLogByIid ( $value ['iid'], 'desc' );
  521. }
  522. return $list;
  523. }
  524. function getInvoiceByUntreadStatusPage($limit = 0, $con = "", $desc = 'desc') {
  525. if (empty ( $limit ) || empty ( $con ))
  526. return array ();
  527. $list = $this->find ( array (
  528. 'where' => $con,
  529. 'limit' => $limit,
  530. $desc => 'iid',
  531. 'asArray' => TRUE
  532. ) );
  533. Doo::loadClass ( 'XDeode' );
  534. $XDeode = new XDeode ( 5 );
  535. Doo::loadModel ( 'invoiceReceivables' );
  536. $invoiceReceivables = new invoiceReceivables ();
  537. Doo::loadModel ( 'invoiceOperationLog' );
  538. $invoiceOperationLog = new invoiceOperationLog ();
  539. foreach ( $list as $key => $value ) {
  540. $list [$key] ['iidKey'] = $this->authcode ( $value ['iid'], '' );
  541. $list [$key] ['iidKeyK'] = $XDeode->encode ( $value ['iid'] );
  542. $list [$key] ['untreadCompany'] = '';
  543. $list [$key] ['untreadCom'] = '';
  544. if (! empty ( $value ['untreadCompany'] )) {
  545. $express = explode ( ":", $value ['untreadCompany'] );
  546. $list [$key] ['untreadCompany'] = $express [0];
  547. $list [$key] ['untreadCom'] = $express [1];
  548. }
  549. $list [$key] ['irList'] = $invoiceReceivables->getInvoiceReceivablesInIridString ( $value ['irid'] );
  550. $list [$key] ['operationLog'] = $invoiceOperationLog->getInvoiceOperationLogByIid ( $value ['iid'], 'desc' );
  551. $list [$key] ['OperationLog'] = $invoiceOperationLog->getInvoiceOperationByStatus ( $value ['iid'], 6 );
  552. $list [$key] ['sumPrice'] = 0;
  553. if (! empty ( $list [$key] ['irList'] ))
  554. $list [$key] ['sumPrice'] = $list [$key] ['irList'] [0] ['sumPrice'];
  555. // $list [$key] ['operationLog'] = $invoiceOperationLog->getInvoiceOperationLogByIid ( $value ['iid'], 'desc' );
  556. }
  557. return $list;
  558. }
  559. /**
  560. * 加密或解密指定字符串
  561. *
  562. * @param string $string 要加密或解密的字符串
  563. * @param string $operation 当取值为'DECODE'时表示解密,否则为加密
  564. * @param string $key 加解密的key
  565. * @param $expiry 超时值
  566. *
  567. */
  568. function authcode($string, $operation = 'DECODE', $key = '', $expiry = 0) {
  569. $ckey_length = 4;
  570. if (! $key) {
  571. $key = $this->INVOICEKEY;
  572. }
  573. $key = md5 ( $key );
  574. $keya = md5 ( substr ( $key, 0, 16 ) );
  575. $keyb = md5 ( substr ( $key, 16, 16 ) );
  576. $keyc = $ckey_length ? ($operation == 'DECODE' ? substr ( $string, 0, $ckey_length ) : substr ( md5 ( microtime () ), - $ckey_length )) : '';
  577. $cryptkey = $keya . md5 ( $keya . $keyc );
  578. $key_length = strlen ( $cryptkey );
  579. $string = $operation == 'DECODE' ? base64_decode ( substr ( $string, $ckey_length ) ) : sprintf ( '%010d', $expiry ? $expiry + time () : 0 ) . substr ( md5 ( $string . $keyb ), 0, 16 ) . $string;
  580. $string_length = strlen ( $string );
  581. $result = '';
  582. $box = range ( 0, 255 );
  583. $rndkey = array ();
  584. for($i = 0; $i <= 255; $i ++) {
  585. $rndkey [$i] = ord ( $cryptkey [$i % $key_length] );
  586. }
  587. for($j = $i = 0; $i < 256; $i ++) {
  588. $j = ($j + $box [$i] + $rndkey [$i]) % 256;
  589. $tmp = $box [$i];
  590. $box [$i] = $box [$j];
  591. $box [$j] = $tmp;
  592. }
  593. for($a = $j = $i = 0; $i < $string_length; $i ++) {
  594. $a = ($a + 1) % 256;
  595. $j = ($j + $box [$a]) % 256;
  596. $tmp = $box [$a];
  597. $box [$a] = $box [$j];
  598. $box [$j] = $tmp;
  599. $result .= chr ( ord ( $string [$i] ) ^ ($box [($box [$a] + $box [$j]) % 256]) );
  600. }
  601. if ($operation == 'DECODE') {
  602. if ((substr ( $result, 0, 10 ) == 0 || substr ( $result, 0, 10 ) - time () > 0) && substr ( $result, 10, 16 ) == substr ( md5 ( substr ( $result, 26 ) . $keyb ), 0, 16 )) {
  603. return substr ( $result, 26 );
  604. } else {
  605. return '';
  606. }
  607. } else {
  608. return $keyc . str_replace ( '=', '', base64_encode ( $result ) );
  609. }
  610. }
  611. }
  612. ?>