> php教程 > php手册 > 用PHP实现一个高效安全的ftp服务器(二)

用PHP实现一个高效安全的ftp服务器(二)

WBOY
풀어 주다: 2016-07-06 13:31:03
원래의
1277명이 탐색했습니다.

接前文。 1.实现用户类CUser。 用户的存储采用文本形式,将用户数组进行json编码。 用户文件格式: * array ( * 'user1' = array ( * 'pass'='', * 'group'='', * 'home'='/home/ftp/', // ftp主目录 * 'active'= true , * 'expired='2015-12-12' , * 'descr

接前文。

 

 1.实现用户类CUser。

  用户的存储采用文本形式,将用户数组进行json编码。  

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

<span style="color: #000000;">用户文件格式:

 </span>* <span style="color: #0000ff;">array</span><span style="color: #000000;">(

 </span>*         'user1' => <span style="color: #0000ff;">array</span><span style="color: #000000;">(

 </span>*             'pass'=>'',

 *             'group'=>'',

 *             'home'=>'/home/ftp/', <span style="color: #008000;">//</span><span style="color: #008000;">ftp主目录</span>

 *             'active'=><span style="color: #0000ff;">true</span>,

 *             'expired=>'2015-12-12'<span style="color: #000000;">,

 *             </span>'description'=>''<span style="color: #000000;">,

 *             </span>'email' => ''<span style="color: #000000;">,

 *             </span>'folder'<span style="color: #000000;">=>array(

 *                     //可以列出主目录下的文件和目录,但不能创建和删除,也不能进入主目录下的目录

 *                     //前1-5位是文件权限,6-9是文件夹权限,10是否继承(inherit)

 *                     array(</span>'path'=>'/home/ftp/','access'=>'RWANDLCNDI'<span style="color: #000000;">),

 *                     //可以列出/home/ftp/a/下的文件和目录,可以创建和删除,可以进入/home/ftp/a/下的子目录,可以创建和删除。

 *                     array(</span>'path'=>'/home/ftp/a/','access'=>'RWAND-----'<span style="color: #000000;">),

 *             ),

 *             </span>'ip'<span style="color: #000000;">=>array(

 *                 </span>'allow'<span style="color: #000000;">=>array(ip1,ip2,...),//支持*通配符: 192.168.0.*

 *                 </span>'deny'<span style="color: #000000;">=>array(ip1,ip2,...)

 *             )

 *         )

 * )

 *

 * 组文件格式:

 * array(

 *         </span>'group1'<span style="color: #000000;">=>array(

 *             </span>'home'=>'/home/ftp/dept1/'<span style="color: #000000;">,

 *             </span>'folder'<span style="color: #000000;">=>array(

 *

 *             ),

 *             </span>'ip'<span style="color: #000000;">=>array(

 *                 </span>'allow'<span style="color: #000000;">=>array(ip1,ip2,...),

 *                 </span>'deny'<span style="color: #000000;">=>array(ip1,ip2,...)

 *            )

 *         )

 * )</span>

로그인 후 복사

 

  文件夹和文件的权限说明:

     

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

* 文件权限

* R读 : 允许用户读取(即下载)文件。该权限不允许用户列出目录内容,执行该操作需要列表权限。

* W写: 允许用户写入(即上传)文件。该权限不允许用户修改现有的文件,执行该操作需要追加权限。

* A追加: 允许用户向现有文件中追加数据。该权限通常用于使用户能够对部分上传的文件进行续传。

* N重命名: 允许用户重命名现有的文件。

* D删除: 允许用户删除文件。

*

* 目录权限

* L列表: 允许用户列出目录中包含的文件。

* C创建: 允许用户在目录中新建子目录。

* N重命名: 允许用户在目录中重命名现有子目录。

* D删除: 允许用户在目录中删除现有子目录。注意: 如果目录包含文件,用户要删除目录还需要具有删除文件权限。

*

* 子目录权限

* I继承: 允许所有子目录继承其父目录具有的相同权限。继承权限适用于大多数情况,但是如果访问必须受限于子文件夹,例如实施强制访问控制(Mandatory Access Control)时,则取消继承并为文件夹逐一授予权限。

*

로그인 후 복사

  实现代码如下:  

  

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

258

259

260

261

262

263

264

265

266

267

268

269

270

271

272

273

274

275

276

277

278

279

280

281

282

283

284

285

286

287

288

289

290

291

292

293

294

295

296

297

298

299

300

301

302

303

304

305

306

307

308

309

310

311

312

313

314

315

316

317

318

319

320

321

322

323

324

325

326

327

328

329

330

331

332

333

334

335

336

337

338

339

340

341

342

343

344

345

346

347

348

349

350

351

352

353

354

355

356

357

358

359

360

361

362

363

364

365

366

367

368

369

370

371

372

373

374

375

376

377

378

379

380

381

382

383

384

385

386

387

388

389

390

391

392

393

394

395

396

397

398

399

400

401

402

403

404

405

406

407

408

409

410

411

412

413

414

415

416

417

418

419

420

421

422

423

424

425

426

427

428

429

430

431

432

433

434

435

436

437

438

439

440

441

442

443

444

445

446

447

448

449

450

451

452

453

454

455

456

457

458

459

460

461

462

463

464

465

466

467

468

469

470

471

472

473

474

475

476

477

478

479

480

481

482

483

484

485

486

487

488

489

490

491

492

493

494

495

496

497

498

499

500

501

502

503

504

505

506

507

508

509

510

511

512

513

514

515

516

517

518

519

520

521

522

523

524

525

526

527

528

529

530

531

532

533

534

535

536

537

538

539

540

541

542

543

544

545

546

547

548

549

550

551

552

553

554

555

<span style="color: #0000ff;">class</span><span style="color: #000000;"> User{

     

    </span><span style="color: #0000ff;">const</span> I = 1;    <span style="color: #008000;">//</span><span style="color: #008000;"> inherit</span>

     

    <span style="color: #0000ff;">const</span> FD = 2;    <span style="color: #008000;">//</span><span style="color: #008000;"> folder delete</span>

     

    <span style="color: #0000ff;">const</span> FN = 4;    <span style="color: #008000;">//</span><span style="color: #008000;"> folder rename</span>

     

    <span style="color: #0000ff;">const</span> FC = 8;    <span style="color: #008000;">//</span><span style="color: #008000;"> folder create</span>

     

    <span style="color: #0000ff;">const</span> FL = 16;    <span style="color: #008000;">//</span><span style="color: #008000;"> folder list</span>

     

    <span style="color: #0000ff;">const</span> D = 32;    <span style="color: #008000;">//</span><span style="color: #008000;"> file delete</span>

     

    <span style="color: #0000ff;">const</span> N = 64;    <span style="color: #008000;">//</span><span style="color: #008000;"> file rename</span>

     

    <span style="color: #0000ff;">const</span> A = 128;    <span style="color: #008000;">//</span><span style="color: #008000;"> file append</span>

     

    <span style="color: #0000ff;">const</span> W = 256;    <span style="color: #008000;">//</span><span style="color: #008000;"> file write (upload)</span>

     

    <span style="color: #0000ff;">const</span> R = 512;    <span style="color: #008000;">//</span><span style="color: #008000;"> file read (download)    </span>

     

    <span style="color: #0000ff;">PRivate</span> <span style="color: #800080;">$hash_salt</span> = ''<span style="color: #000000;">;

     

    </span><span style="color: #0000ff;">private</span> <span style="color: #800080;">$user_file</span><span style="color: #000000;">;

     

    </span><span style="color: #0000ff;">private</span> <span style="color: #800080;">$group_file</span><span style="color: #000000;">;

     

    </span><span style="color: #0000ff;">private</span> <span style="color: #800080;">$users</span> = <span style="color: #0000ff;">array</span><span style="color: #000000;">();

     

    </span><span style="color: #0000ff;">private</span> <span style="color: #800080;">$groups</span> = <span style="color: #0000ff;">array</span><span style="color: #000000;">();

     

    </span><span style="color: #0000ff;">private</span> <span style="color: #800080;">$file_hash</span> = ''<span style="color: #000000;">;

     

    </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> __construct(){

        </span><span style="color: #800080;">$this</span>->user_file = BASE_PATH.'/conf/users'<span style="color: #000000;">;

        </span><span style="color: #800080;">$this</span>->group_file = BASE_PATH.'/conf/groups'<span style="color: #000000;">;

        </span><span style="color: #800080;">$this</span>-><span style="color: #000000;">reload();

    }

     

    </span><span style="color: #008000;">/*</span><span style="color: #008000;">*

     * 返回权限表达式

     * @param int $access

     * @return string

     </span><span style="color: #008000;">*/</span>

    <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">function</span> AC(<span style="color: #800080;">$access</span><span style="color: #000000;">){

        </span><span style="color: #800080;">$str</span> = ''<span style="color: #000000;">;

        </span><span style="color: #800080;">$char</span> = <span style="color: #0000ff;">array</span>('R','W','A','N','D','L','C','N','D','I'<span style="color: #000000;">);

        </span><span style="color: #0000ff;">for</span>(<span style="color: #800080;">$i</span> = 0; <span style="color: #800080;">$i</span> $i++<span style="color: #000000;">){

            </span><span style="color: #0000ff;">if</span>(<span style="color: #800080;">$access</span> & <span style="color: #008080;">pow</span>(2,9-<span style="color: #800080;">$i</span>))<span style="color: #800080;">$str</span>.= <span style="color: #800080;">$char</span>[<span style="color: #800080;">$i</span>];<span style="color: #0000ff;">else</span> <span style="color: #800080;">$str</span>.= '-'<span style="color: #000000;">;

        }

        </span><span style="color: #0000ff;">return</span> <span style="color: #800080;">$str</span><span style="color: #000000;">;

    }

     

    </span><span style="color: #008000;">/*</span><span style="color: #008000;">*

     * 加载用户数据

     </span><span style="color: #008000;">*/</span>

    <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> reload(){

        </span><span style="color: #800080;">$user_file_hash</span> = <span style="color: #008080;">md5_file</span>(<span style="color: #800080;">$this</span>-><span style="color: #000000;">user_file);

        </span><span style="color: #800080;">$group_file_hash</span> = <span style="color: #008080;">md5_file</span>(<span style="color: #800080;">$this</span>-><span style="color: #000000;">group_file);       

        </span><span style="color: #0000ff;">if</span>(<span style="color: #800080;">$this</span>->file_hash != <span style="color: #008080;">md5</span>(<span style="color: #800080;">$user_file_hash</span>.<span style="color: #800080;">$group_file_hash</span><span style="color: #000000;">)){

            </span><span style="color: #0000ff;">if</span>((<span style="color: #800080;">$user</span> = <span style="color: #008080;">file_get_contents</span>(<span style="color: #800080;">$this</span>->user_file)) !== <span style="color: #0000ff;">false</span><span style="color: #000000;">){

                </span><span style="color: #800080;">$this</span>->users = json_decode(<span style="color: #800080;">$user</span>,<span style="color: #0000ff;">true</span><span style="color: #000000;">);

                </span><span style="color: #0000ff;">if</span>(<span style="color: #800080;">$this</span>-><span style="color: #000000;">users){

                    </span><span style="color: #008000;">//</span><span style="color: #008000;">folder排序</span>

                    <span style="color: #0000ff;">foreach</span> (<span style="color: #800080;">$this</span>->users <span style="color: #0000ff;">as</span> <span style="color: #800080;">$user</span>=><span style="color: #800080;">$profile</span><span style="color: #000000;">){

                        </span><span style="color: #0000ff;">if</span>(<span style="color: #0000ff;">isset</span>(<span style="color: #800080;">$profile</span>['folder'<span style="color: #000000;">])){

                            </span><span style="color: #800080;">$this</span>->users[<span style="color: #800080;">$user</span>]['folder'] = <span style="color: #800080;">$this</span>->sortFolder(<span style="color: #800080;">$profile</span>['folder'<span style="color: #000000;">]);

                        }

                    }

                }

            }

            </span><span style="color: #0000ff;">if</span>((<span style="color: #800080;">$group</span> = <span style="color: #008080;">file_get_contents</span>(<span style="color: #800080;">$this</span>->group_file)) !== <span style="color: #0000ff;">false</span><span style="color: #000000;">){

                </span><span style="color: #800080;">$this</span>->groups = json_decode(<span style="color: #800080;">$group</span>,<span style="color: #0000ff;">true</span><span style="color: #000000;">);

                </span><span style="color: #0000ff;">if</span>(<span style="color: #800080;">$this</span>-><span style="color: #000000;">groups){

                    </span><span style="color: #008000;">//</span><span style="color: #008000;">folder排序</span>

                    <span style="color: #0000ff;">foreach</span> (<span style="color: #800080;">$this</span>->groups <span style="color: #0000ff;">as</span> <span style="color: #800080;">$group</span>=><span style="color: #800080;">$profile</span><span style="color: #000000;">){                           

                        </span><span style="color: #0000ff;">if</span>(<span style="color: #0000ff;">isset</span>(<span style="color: #800080;">$profile</span>['folder'<span style="color: #000000;">])){                       

                            </span><span style="color: #800080;">$this</span>->groups[<span style="color: #800080;">$group</span>]['folder'] = <span style="color: #800080;">$this</span>->sortFolder(<span style="color: #800080;">$profile</span>['folder'<span style="color: #000000;">]);

                        }

                    }

                }

            }

            </span><span style="color: #800080;">$this</span>->file_hash = <span style="color: #008080;">md5</span>(<span style="color: #800080;">$user_file_hash</span>.<span style="color: #800080;">$group_file_hash</span><span style="color: #000000;">);           

        }

    }

     

    </span><span style="color: #008000;">/*</span><span style="color: #008000;">*

     * 对folder进行排序

     * @return array

     </span><span style="color: #008000;">*/</span>

    <span style="color: #0000ff;">private</span> <span style="color: #0000ff;">function</span> sortFolder(<span style="color: #800080;">$folder</span><span style="color: #000000;">){

        </span><span style="color: #008080;">uasort</span>(<span style="color: #800080;">$folder</span>, <span style="color: #0000ff;">function</span>(<span style="color: #800080;">$a</span>,<span style="color: #800080;">$b</span><span style="color: #000000;">){

            </span><span style="color: #0000ff;">return</span> <span style="color: #008080;">strnatcmp</span>(<span style="color: #800080;">$a</span>['path'], <span style="color: #800080;">$b</span>['path'<span style="color: #000000;">]);

        });   

        </span><span style="color: #800080;">$result</span> = <span style="color: #0000ff;">array</span><span style="color: #000000;">();

        </span><span style="color: #0000ff;">foreach</span> (<span style="color: #800080;">$folder</span> <span style="color: #0000ff;">as</span> <span style="color: #800080;">$v</span><span style="color: #000000;">){

            </span><span style="color: #800080;">$result</span>[] = <span style="color: #800080;">$v</span><span style="color: #000000;">;

        }   

        </span><span style="color: #0000ff;">return</span> <span style="color: #800080;">$result</span><span style="color: #000000;">;

    }

     

    </span><span style="color: #008000;">/*</span><span style="color: #008000;">*

     * 保存用户数据

     </span><span style="color: #008000;">*/</span>

    <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> save(){

        </span><span style="color: #008080;">file_put_contents</span>(<span style="color: #800080;">$this</span>->user_file, json_encode(<span style="color: #800080;">$this</span>->users),<span style="color: #000000;">LOCK_EX);

        </span><span style="color: #008080;">file_put_contents</span>(<span style="color: #800080;">$this</span>->group_file, json_encode(<span style="color: #800080;">$this</span>->groups),<span style="color: #000000;">LOCK_EX);

    }

     

    </span><span style="color: #008000;">/*</span><span style="color: #008000;">*

     * 添加用户

     * @param string $user

     * @param string $pass

     * @param string $home

     * @param string $expired

     * @param boolean $active

     * @param string $group

     * @param string $description

     * @param string $email

     * @return boolean

     </span><span style="color: #008000;">*/</span>

    <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span> addUser(<span style="color: #800080;">$user</span>,<span style="color: #800080;">$pass</span>,<span style="color: #800080;">$home</span>,<span style="color: #800080;">$expired</span>,<span style="color: #800080;">$active</span>=<span style="color: #0000ff;">true</span>,<span style="color: #800080;">$group</span>='',<span style="color: #800080;">$description</span>='',<span style="color: #800080;">$email</span> = ''<span style="color: #000000;">){

        </span><span style="color: #800080;">$user</span> = <span style="color: #008080;">strtolower</span>(<span style="color: #800080;">$user</span><span style="color: #000000;">);

        </span><span style="color: #0000ff;">if</span>(<span style="color: #0000ff;">isset</span>(<span style="color: #800080;">$this</span>->users[<span style="color: #800080;">$user</span>]) || <span style="color: #0000ff;">empty</span>(<span style="color: #800080;">$user</span><span style="color: #000000;">)){

            </span><span style="color: #0000ff;">return</span> <span style="color: #0000ff;">false</span><span style="color: #000000;">;

        }       

        </span><span style="color: #800080;">$this</span>->users[<span style="color: #800080;">$user</span>] = <span style="color: #0000ff;">array</span><span style="color: #000000;">(

                </span>'pass' => <span style="color: #008080;">md5</span>(<span style="color: #800080;">$user</span>.<span style="color: #800080;">$this</span>->hash_salt.<span style="color: #800080;">$pass</span>),

                'home' => <span style="color: #800080;">$home</span>,

                'expired' => <span style="color: #800080;">$expired</span>,

                'active' => <span style="color: #800080;">$active</span>,

                'group' => <span style="color: #800080;">$group</span>,

                'description' => <span style="color: #800080;">$description</span>,

                'email' => <span style="color: #800080;">$email</span>,<span style="color: #000000;">

        );

        </span><span style="color: #0000ff;">return</span> <span style="color: #0000ff;">true</span><span style="color: #000000;">;

    }

     

    </span><span style="color: #008000;">/*</span><span style="color: #008000;">*

     * 设置用户资料

     * @param string $user

     * @param array $profile

     * @return boolean

     </span><span style="color: #008000;">*/</span>

    <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span> setUserProfile(<span style="color: #800080;">$user</span>,<span style="color: #800080;">$profile</span><span style="color: #000000;">){

        </span><span style="color: #800080;">$user</span> = <span style="color: #008080;">strtolower</span>(<span style="color: #800080;">$user</span><span style="color: #000000;">);

        </span><span style="color: #0000ff;">if</span>(<span style="color: #008080;">is_array</span>(<span style="color: #800080;">$profile</span>) && <span style="color: #0000ff;">isset</span>(<span style="color: #800080;">$this</span>->users[<span style="color: #800080;">$user</span><span style="color: #000000;">])){

            </span><span style="color: #0000ff;">if</span>(<span style="color: #0000ff;">isset</span>(<span style="color: #800080;">$profile</span>['pass'<span style="color: #000000;">])){

                </span><span style="color: #800080;">$profile</span>['pass'] = <span style="color: #008080;">md5</span>(<span style="color: #800080;">$user</span>.<span style="color: #800080;">$this</span>->hash_salt.<span style="color: #800080;">$profile</span>['pass'<span style="color: #000000;">]);

            }

            </span><span style="color: #0000ff;">if</span>(<span style="color: #0000ff;">isset</span>(<span style="color: #800080;">$profile</span>['active'<span style="color: #000000;">])){

                </span><span style="color: #0000ff;">if</span>(!<span style="color: #008080;">is_bool</span>(<span style="color: #800080;">$profile</span>['active'<span style="color: #000000;">])){

                    </span><span style="color: #800080;">$profile</span>['active'] = <span style="color: #800080;">$profile</span>['active'] == 'true' ? <span style="color: #0000ff;">true</span> : <span style="color: #0000ff;">false</span><span style="color: #000000;">;

                }

            }           

            </span><span style="color: #800080;">$this</span>->users[<span style="color: #800080;">$user</span>] = <span style="color: #008080;">array_merge</span>(<span style="color: #800080;">$this</span>->users[<span style="color: #800080;">$user</span>],<span style="color: #800080;">$profile</span><span style="color: #000000;">);

            </span><span style="color: #0000ff;">return</span> <span style="color: #0000ff;">true</span><span style="color: #000000;">;

        }

        </span><span style="color: #0000ff;">return</span> <span style="color: #0000ff;">false</span><span style="color: #000000;">;

    }

     

    </span><span style="color: #008000;">/*</span><span style="color: #008000;">*

     * 获取用户资料

     * @param string $user

     * @return multitype:|boolean

     </span><span style="color: #008000;">*/</span>

    <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span> getUserProfile(<span style="color: #800080;">$user</span><span style="color: #000000;">){

        </span><span style="color: #800080;">$user</span> = <span style="color: #008080;">strtolower</span>(<span style="color: #800080;">$user</span><span style="color: #000000;">);

        </span><span style="color: #0000ff;">if</span>(<span style="color: #0000ff;">isset</span>(<span style="color: #800080;">$this</span>->users[<span style="color: #800080;">$user</span><span style="color: #000000;">])){

            </span><span style="color: #0000ff;">return</span> <span style="color: #800080;">$this</span>->users[<span style="color: #800080;">$user</span><span style="color: #000000;">];

        }

        </span><span style="color: #0000ff;">return</span> <span style="color: #0000ff;">false</span><span style="color: #000000;">;

    }

    </span><span style="color: #008000;">/*</span><span style="color: #008000;">*

     * 删除用户

     * @param string $user

     * @return boolean

     </span><span style="color: #008000;">*/</span>

    <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span> delUser(<span style="color: #800080;">$user</span><span style="color: #000000;">){

        </span><span style="color: #800080;">$user</span> = <span style="color: #008080;">strtolower</span>(<span style="color: #800080;">$user</span><span style="color: #000000;">);

        </span><span style="color: #0000ff;">if</span>(<span style="color: #0000ff;">isset</span>(<span style="color: #800080;">$this</span>->users[<span style="color: #800080;">$user</span><span style="color: #000000;">])){

            </span><span style="color: #0000ff;">unset</span>(<span style="color: #800080;">$this</span>->users[<span style="color: #800080;">$user</span><span style="color: #000000;">]);

            </span><span style="color: #0000ff;">return</span> <span style="color: #0000ff;">true</span><span style="color: #000000;">;

        }

        </span><span style="color: #0000ff;">return</span> <span style="color: #0000ff;">false</span><span style="color: #000000;">;

    }

     

    </span><span style="color: #008000;">/*</span><span style="color: #008000;">*

     * 获取用户列表

     * @return array

     </span><span style="color: #008000;">*/</span>

    <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> getUserList(){

        </span><span style="color: #800080;">$list</span> = <span style="color: #0000ff;">array</span><span style="color: #000000;">();

        </span><span style="color: #0000ff;">if</span>(<span style="color: #800080;">$this</span>-><span style="color: #000000;">users){

            </span><span style="color: #0000ff;">foreach</span> (<span style="color: #800080;">$this</span>->users <span style="color: #0000ff;">as</span> <span style="color: #800080;">$user</span>=><span style="color: #800080;">$profile</span><span style="color: #000000;">){

                </span><span style="color: #800080;">$list</span>[] = <span style="color: #800080;">$user</span><span style="color: #000000;">;

            }

        }

        </span><span style="color: #008080;">sort</span>(<span style="color: #800080;">$list</span><span style="color: #000000;">);

        </span><span style="color: #0000ff;">return</span> <span style="color: #800080;">$list</span><span style="color: #000000;">;

    }

     

    </span><span style="color: #008000;">/*</span><span style="color: #008000;">*

     * 添加组

     * @param string $group

     * @param string $home

     * @return boolean

     </span><span style="color: #008000;">*/</span>

    <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span> addGroup(<span style="color: #800080;">$group</span>,<span style="color: #800080;">$home</span><span style="color: #000000;">){

        </span><span style="color: #800080;">$group</span> = <span style="color: #008080;">strtolower</span>(<span style="color: #800080;">$group</span><span style="color: #000000;">);

        </span><span style="color: #0000ff;">if</span>(<span style="color: #0000ff;">isset</span>(<span style="color: #800080;">$this</span>->groups[<span style="color: #800080;">$group</span><span style="color: #000000;">])){

            </span><span style="color: #0000ff;">return</span> <span style="color: #0000ff;">false</span><span style="color: #000000;">;

        }

        </span><span style="color: #800080;">$this</span>->groups[<span style="color: #800080;">$group</span>] = <span style="color: #0000ff;">array</span><span style="color: #000000;">(

                </span>'home' => <span style="color: #800080;">$home</span><span style="color: #000000;">

        );

        </span><span style="color: #0000ff;">return</span> <span style="color: #0000ff;">true</span><span style="color: #000000;">;

    }

     

    </span><span style="color: #008000;">/*</span><span style="color: #008000;">*

     * 设置组资料

     * @param string $group

     * @param array $profile

     * @return boolean

     </span><span style="color: #008000;">*/</span>

    <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span> setGroupProfile(<span style="color: #800080;">$group</span>,<span style="color: #800080;">$profile</span><span style="color: #000000;">){

        </span><span style="color: #800080;">$group</span> = <span style="color: #008080;">strtolower</span>(<span style="color: #800080;">$group</span><span style="color: #000000;">);

        </span><span style="color: #0000ff;">if</span>(<span style="color: #008080;">is_array</span>(<span style="color: #800080;">$profile</span>) && <span style="color: #0000ff;">isset</span>(<span style="color: #800080;">$this</span>->groups[<span style="color: #800080;">$group</span><span style="color: #000000;">])){

            </span><span style="color: #800080;">$this</span>->groups[<span style="color: #800080;">$group</span>] = <span style="color: #008080;">array_merge</span>(<span style="color: #800080;">$this</span>->groups[<span style="color: #800080;">$group</span>],<span style="color: #800080;">$profile</span><span style="color: #000000;">);

            </span><span style="color: #0000ff;">return</span> <span style="color: #0000ff;">true</span><span style="color: #000000;">;

        }

        </span><span style="color: #0000ff;">return</span> <span style="color: #0000ff;">false</span><span style="color: #000000;">;

    }

     

    </span><span style="color: #008000;">/*</span><span style="color: #008000;">*

     * 获取组资料

     * @param string $group

     * @return multitype:|boolean

     </span><span style="color: #008000;">*/</span>

    <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span> getGroupProfile(<span style="color: #800080;">$group</span><span style="color: #000000;">){

        </span><span style="color: #800080;">$group</span> = <span style="color: #008080;">strtolower</span>(<span style="color: #800080;">$group</span><span style="color: #000000;">);

        </span><span style="color: #0000ff;">if</span>(<span style="color: #0000ff;">isset</span>(<span style="color: #800080;">$this</span>->groups[<span style="color: #800080;">$group</span><span style="color: #000000;">])){

            </span><span style="color: #0000ff;">return</span> <span style="color: #800080;">$this</span>->groups[<span style="color: #800080;">$group</span><span style="color: #000000;">];

        }

        </span><span style="color: #0000ff;">return</span> <span style="color: #0000ff;">false</span><span style="color: #000000;">;

    }

     

    </span><span style="color: #008000;">/*</span><span style="color: #008000;">*

     * 删除组

     * @param string $group

     * @return boolean

     </span><span style="color: #008000;">*/</span>

    <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span> delGroup(<span style="color: #800080;">$group</span><span style="color: #000000;">){

        </span><span style="color: #800080;">$group</span> = <span style="color: #008080;">strtolower</span>(<span style="color: #800080;">$group</span><span style="color: #000000;">);

        </span><span style="color: #0000ff;">if</span>(<span style="color: #0000ff;">isset</span>(<span style="color: #800080;">$this</span>->groups[<span style="color: #800080;">$group</span><span style="color: #000000;">])){

            </span><span style="color: #0000ff;">unset</span>(<span style="color: #800080;">$this</span>->groups[<span style="color: #800080;">$group</span><span style="color: #000000;">]);

            </span><span style="color: #0000ff;">foreach</span> (<span style="color: #800080;">$this</span>->users <span style="color: #0000ff;">as</span> <span style="color: #800080;">$user</span> => <span style="color: #800080;">$profile</span><span style="color: #000000;">){

                </span><span style="color: #0000ff;">if</span>(<span style="color: #800080;">$profile</span>['group'] == <span style="color: #800080;">$group</span><span style="color: #000000;">)

                    </span><span style="color: #800080;">$this</span>->users[<span style="color: #800080;">$user</span>]['group'] = ''<span style="color: #000000;">;

            }

            </span><span style="color: #0000ff;">return</span> <span style="color: #0000ff;">true</span><span style="color: #000000;">;

        }

        </span><span style="color: #0000ff;">return</span> <span style="color: #0000ff;">false</span><span style="color: #000000;">;

    }

     

    </span><span style="color: #008000;">/*</span><span style="color: #008000;">*

     * 获取组列表

     * @return array

     </span><span style="color: #008000;">*/</span>

    <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> getGroupList(){

        </span><span style="color: #800080;">$list</span> = <span style="color: #0000ff;">array</span><span style="color: #000000;">();

        </span><span style="color: #0000ff;">if</span>(<span style="color: #800080;">$this</span>-><span style="color: #000000;">groups){

            </span><span style="color: #0000ff;">foreach</span> (<span style="color: #800080;">$this</span>->groups <span style="color: #0000ff;">as</span> <span style="color: #800080;">$group</span>=><span style="color: #800080;">$profile</span><span style="color: #000000;">){

                </span><span style="color: #800080;">$list</span>[] = <span style="color: #800080;">$group</span><span style="color: #000000;">;

            }

        }

        </span><span style="color: #008080;">sort</span>(<span style="color: #800080;">$list</span><span style="color: #000000;">);

        </span><span style="color: #0000ff;">return</span> <span style="color: #800080;">$list</span><span style="color: #000000;">;

    }

     

    </span><span style="color: #008000;">/*</span><span style="color: #008000;">*

     * 获取组用户列表

     * @param string $group

     * @return array

     </span><span style="color: #008000;">*/</span>

    <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span> getUserListOfGroup(<span style="color: #800080;">$group</span><span style="color: #000000;">){

        </span><span style="color: #800080;">$list</span> = <span style="color: #0000ff;">array</span><span style="color: #000000;">();

        </span><span style="color: #0000ff;">if</span>(<span style="color: #0000ff;">isset</span>(<span style="color: #800080;">$this</span>->groups[<span style="color: #800080;">$group</span>]) && <span style="color: #800080;">$this</span>-><span style="color: #000000;">users){

            </span><span style="color: #0000ff;">foreach</span> (<span style="color: #800080;">$this</span>->users <span style="color: #0000ff;">as</span> <span style="color: #800080;">$user</span>=><span style="color: #800080;">$profile</span><span style="color: #000000;">){

                </span><span style="color: #0000ff;">if</span>(<span style="color: #0000ff;">isset</span>(<span style="color: #800080;">$profile</span>['group']) && <span style="color: #800080;">$profile</span>['group'] == <span style="color: #800080;">$group</span><span style="color: #000000;">){

                    </span><span style="color: #800080;">$list</span>[] = <span style="color: #800080;">$user</span><span style="color: #000000;">;

                }

            }

        }

        </span><span style="color: #008080;">sort</span>(<span style="color: #800080;">$list</span><span style="color: #000000;">);

        </span><span style="color: #0000ff;">return</span> <span style="color: #800080;">$list</span><span style="color: #000000;">;

    }

     

    </span><span style="color: #008000;">/*</span><span style="color: #008000;">*

     * 用户验证

     * @param string $user

     * @param string $pass

     * @param string $ip

     * @return boolean

     </span><span style="color: #008000;">*/</span>

    <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span> checkUser(<span style="color: #800080;">$user</span>,<span style="color: #800080;">$pass</span>,<span style="color: #800080;">$ip</span> = ''<span style="color: #000000;">){

        </span><span style="color: #800080;">$this</span>-><span style="color: #000000;">reload();

        </span><span style="color: #800080;">$user</span> = <span style="color: #008080;">strtolower</span>(<span style="color: #800080;">$user</span><span style="color: #000000;">);

        </span><span style="color: #0000ff;">if</span>(<span style="color: #0000ff;">isset</span>(<span style="color: #800080;">$this</span>->users[<span style="color: #800080;">$user</span><span style="color: #000000;">])){

            </span><span style="color: #0000ff;">if</span>(<span style="color: #800080;">$this</span>->users[<span style="color: #800080;">$user</span>]['active'] && <span style="color: #008080;">time</span>() strtotime(<span style="color: #800080;">$this</span>->users[<span style="color: #800080;">$user</span>]['expired'<span style="color: #000000;">])

                 </span>&& <span style="color: #800080;">$this</span>->users[<span style="color: #800080;">$user</span>]['pass'] == <span style="color: #008080;">md5</span>(<span style="color: #800080;">$user</span>.<span style="color: #800080;">$this</span>->hash_salt.<span style="color: #800080;">$pass</span><span style="color: #000000;">)){

                </span><span style="color: #0000ff;">if</span>(<span style="color: #0000ff;">empty</span>(<span style="color: #800080;">$ip</span><span style="color: #000000;">)){

                    </span><span style="color: #0000ff;">return</span> <span style="color: #0000ff;">true</span><span style="color: #000000;">;

                }</span><span style="color: #0000ff;">else</span><span style="color: #000000;">{

                    </span><span style="color: #008000;">//</span><span style="color: #008000;">ip验证</span>

                    <span style="color: #0000ff;">return</span> <span style="color: #800080;">$this</span>->checkIP(<span style="color: #800080;">$user</span>, <span style="color: #800080;">$ip</span><span style="color: #000000;">);

                }

            }</span><span style="color: #0000ff;">else</span><span style="color: #000000;">{

                </span><span style="color: #0000ff;">return</span> <span style="color: #0000ff;">false</span><span style="color: #000000;">;

            }       

        }

        </span><span style="color: #0000ff;">return</span> <span style="color: #0000ff;">false</span><span style="color: #000000;">;

    }

     

    </span><span style="color: #008000;">/*</span><span style="color: #008000;">*

     * basic auth

     * @param string $base64   

     </span><span style="color: #008000;">*/</span>

    <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span> checkUserBasicAuth(<span style="color: #800080;">$base64</span><span style="color: #000000;">){

        </span><span style="color: #800080;">$base64</span> = <span style="color: #008080;">trim</span>(<span style="color: #008080;">str_replace</span>('Basic ', '', <span style="color: #800080;">$base64</span><span style="color: #000000;">));

        </span><span style="color: #800080;">$str</span> = <span style="color: #008080;">base64_decode</span>(<span style="color: #800080;">$base64</span><span style="color: #000000;">);

        </span><span style="color: #0000ff;">if</span>(<span style="color: #800080;">$str</span> !== <span style="color: #0000ff;">false</span><span style="color: #000000;">){

            </span><span style="color: #0000ff;">list</span>(<span style="color: #800080;">$user</span>,<span style="color: #800080;">$pass</span>) = <span style="color: #008080;">explode</span>(':', <span style="color: #800080;">$str</span>,2<span style="color: #000000;">);

            </span><span style="color: #800080;">$this</span>-><span style="color: #000000;">reload();

            </span><span style="color: #800080;">$user</span> = <span style="color: #008080;">strtolower</span>(<span style="color: #800080;">$user</span><span style="color: #000000;">);

            </span><span style="color: #0000ff;">if</span>(<span style="color: #0000ff;">isset</span>(<span style="color: #800080;">$this</span>->users[<span style="color: #800080;">$user</span><span style="color: #000000;">])){

                </span><span style="color: #800080;">$group</span> = <span style="color: #800080;">$this</span>->users[<span style="color: #800080;">$user</span>]['group'<span style="color: #000000;">];

                </span><span style="color: #0000ff;">if</span>(<span style="color: #800080;">$group</span> == 'admin' && <span style="color: #800080;">$this</span>->users[<span style="color: #800080;">$user</span>]['active'] && <span style="color: #008080;">time</span>() strtotime(<span style="color: #800080;">$this</span>->users[<span style="color: #800080;">$user</span>]['expired'<span style="color: #000000;">])

                </span>&& <span style="color: #800080;">$this</span>->users[<span style="color: #800080;">$user</span>]['pass'] == <span style="color: #008080;">md5</span>(<span style="color: #800080;">$user</span>.<span style="color: #800080;">$this</span>->hash_salt.<span style="color: #800080;">$pass</span><span style="color: #000000;">)){               

                    </span><span style="color: #0000ff;">return</span> <span style="color: #0000ff;">true</span><span style="color: #000000;">;

                }</span><span style="color: #0000ff;">else</span><span style="color: #000000;">{

                    </span><span style="color: #0000ff;">return</span> <span style="color: #0000ff;">false</span><span style="color: #000000;">;

                }

            }

        }

        </span><span style="color: #0000ff;">return</span> <span style="color: #0000ff;">false</span><span style="color: #000000;">;

    }

     

    </span><span style="color: #008000;">/*</span><span style="color: #008000;">*

     * 用户登录ip验证

     * @param string $user

     * @param string $ip

     *

     * 用户的ip权限继承组的IP权限。

     * 匹配规则:

     * 1.进行组允许列表匹配;

     * 2.如同通过,进行组拒绝列表匹配;

     * 3.进行用户允许匹配

     * 4.如果通过,进行用户拒绝匹配

     *

     </span><span style="color: #008000;">*/</span>

    <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span> checkIP(<span style="color: #800080;">$user</span>,<span style="color: #800080;">$ip</span><span style="color: #000000;">){

        </span><span style="color: #800080;">$pass</span> = <span style="color: #0000ff;">false</span><span style="color: #000000;">;

        </span><span style="color: #008000;">//</span><span style="color: #008000;">先进行组验证        </span>

        <span style="color: #800080;">$group</span> = <span style="color: #800080;">$this</span>->users[<span style="color: #800080;">$user</span>]['group'<span style="color: #000000;">];

        </span><span style="color: #008000;">//</span><span style="color: #008000;">组允许匹配</span>

        <span style="color: #0000ff;">if</span>(<span style="color: #0000ff;">isset</span>(<span style="color: #800080;">$this</span>->groups[<span style="color: #800080;">$group</span>]['ip']['allow'<span style="color: #000000;">])){

            </span><span style="color: #0000ff;">foreach</span> (<span style="color: #800080;">$this</span>->groups[<span style="color: #800080;">$group</span>]['ip']['allow'] <span style="color: #0000ff;">as</span> <span style="color: #800080;">$addr</span><span style="color: #000000;">){

                </span><span style="color: #800080;">$pattern</span> = '/'.<span style="color: #008080;">str_replace</span>('*','\d+',<span style="color: #008080;">str_replace</span>('.', '\.', <span style="color: #800080;">$addr</span>)).'/'<span style="color: #000000;">;

                </span><span style="color: #0000ff;">if</span>(<span style="color: #008080;">preg_match</span>(<span style="color: #800080;">$pattern</span>, <span style="color: #800080;">$ip</span>) && !<span style="color: #0000ff;">empty</span>(<span style="color: #800080;">$addr</span><span style="color: #000000;">)){

                    </span><span style="color: #800080;">$pass</span> = <span style="color: #0000ff;">true</span><span style="color: #000000;">;

                    </span><span style="color: #0000ff;">break</span><span style="color: #000000;">;

                }

            }

        }

        </span><span style="color: #008000;">//</span><span style="color: #008000;">如果允许通过,进行拒绝匹配</span>

        <span style="color: #0000ff;">if</span>(<span style="color: #800080;">$pass</span><span style="color: #000000;">){

            </span><span style="color: #0000ff;">if</span>(<span style="color: #0000ff;">isset</span>(<span style="color: #800080;">$this</span>->groups[<span style="color: #800080;">$group</span>]['ip']['deny'<span style="color: #000000;">])){

                </span><span style="color: #0000ff;">foreach</span> (<span style="color: #800080;">$this</span>->groups[<span style="color: #800080;">$group</span>]['ip']['deny'] <span style="color: #0000ff;">as</span> <span style="color: #800080;">$addr</span><span style="color: #000000;">){

                    </span><span style="color: #800080;">$pattern</span> = '/'.<span style="color: #008080;">str_replace</span>('*','\d+',<span style="color: #008080;">str_replace</span>('.', '\.', <span style="color: #800080;">$addr</span>)).'/'<span style="color: #000000;">;

                    </span><span style="color: #0000ff;">if</span>(<span style="color: #008080;">preg_match</span>(<span style="color: #800080;">$pattern</span>, <span style="color: #800080;">$ip</span>) && !<span style="color: #0000ff;">empty</span>(<span style="color: #800080;">$addr</span><span style="color: #000000;">)){

                        </span><span style="color: #800080;">$pass</span> = <span style="color: #0000ff;">false</span><span style="color: #000000;">;

                        </span><span style="color: #0000ff;">break</span><span style="color: #000000;">;

                    }

                }

            }

        }

         

        </span><span style="color: #0000ff;">if</span>(<span style="color: #0000ff;">isset</span>(<span style="color: #800080;">$this</span>->users[<span style="color: #800080;">$user</span>]['ip']['allow'<span style="color: #000000;">])){           

            </span><span style="color: #0000ff;">foreach</span> (<span style="color: #800080;">$this</span>->users[<span style="color: #800080;">$user</span>]['ip']['allow'] <span style="color: #0000ff;">as</span> <span style="color: #800080;">$addr</span><span style="color: #000000;">){

                </span><span style="color: #800080;">$pattern</span> = '/'.<span style="color: #008080;">str_replace</span>('*','\d+',<span style="color: #008080;">str_replace</span>('.', '\.', <span style="color: #800080;">$addr</span>)).'/'<span style="color: #000000;">;

                </span><span style="color: #0000ff;">if</span>(<span style="color: #008080;">preg_match</span>(<span style="color: #800080;">$pattern</span>, <span style="color: #800080;">$ip</span>) && !<span style="color: #0000ff;">empty</span>(<span style="color: #800080;">$addr</span><span style="color: #000000;">)){

                    </span><span style="color: #800080;">$pass</span> = <span style="color: #0000ff;">true</span><span style="color: #000000;">;

                    </span><span style="color: #0000ff;">break</span><span style="color: #000000;">;

                }

            }

        }

        </span><span style="color: #0000ff;">if</span>(<span style="color: #800080;">$pass</span><span style="color: #000000;">){

            </span><span style="color: #0000ff;">if</span>(<span style="color: #0000ff;">isset</span>(<span style="color: #800080;">$this</span>->users[<span style="color: #800080;">$user</span>]['ip']['deny'<span style="color: #000000;">])){

                </span><span style="color: #0000ff;">foreach</span> (<span style="color: #800080;">$this</span>->users[<span style="color: #800080;">$user</span>]['ip']['deny'] <span style="color: #0000ff;">as</span> <span style="color: #800080;">$addr</span><span style="color: #000000;">){

                    </span><span style="color: #800080;">$pattern</span> = '/'.<span style="color: #008080;">str_replace</span>('*','\d+',<span style="color: #008080;">str_replace</span>('.', '\.', <span style="color: #800080;">$addr</span>)).'/'<span style="color: #000000;">;

                    </span><span style="color: #0000ff;">if</span>(<span style="color: #008080;">preg_match</span>(<span style="color: #800080;">$pattern</span>, <span style="color: #800080;">$ip</span>) && !<span style="color: #0000ff;">empty</span>(<span style="color: #800080;">$addr</span><span style="color: #000000;">)){

                        </span><span style="color: #800080;">$pass</span> = <span style="color: #0000ff;">false</span><span style="color: #000000;">;

                        </span><span style="color: #0000ff;">break</span><span style="color: #000000;">;

                    }

                }

            }

        }

        </span><span style="color: #0000ff;">echo</span> <span style="color: #008080;">date</span>('Y-m-d H:i:s')." [debug]\tIP ACCESS:".' '.(<span style="color: #800080;">$pass</span>?'true':'false')."\n"<span style="color: #000000;">;

        </span><span style="color: #0000ff;">return</span> <span style="color: #800080;">$pass</span><span style="color: #000000;">;

    }

     

    </span><span style="color: #008000;">/*</span><span style="color: #008000;">*

     * 获取用户主目录

     * @param string $user

     * @return string

     </span><span style="color: #008000;">*/</span>

    <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span> getHomeDir(<span style="color: #800080;">$user</span><span style="color: #000000;">){

        </span><span style="color: #800080;">$user</span> = <span style="color: #008080;">strtolower</span>(<span style="color: #800080;">$user</span><span style="color: #000000;">);

        </span><span style="color: #800080;">$group</span> = <span style="color: #800080;">$this</span>->users[<span style="color: #800080;">$user</span>]['group'<span style="color: #000000;">];

        </span><span style="color: #800080;">$dir</span> = ''<span style="color: #000000;">;

        </span><span style="color: #0000ff;">if</span>(<span style="color: #800080;">$group</span><span style="color: #000000;">){

            </span><span style="color: #0000ff;">if</span>(<span style="color: #0000ff;">isset</span>(<span style="color: #800080;">$this</span>->groups[<span style="color: #800080;">$group</span>]['home']))<span style="color: #800080;">$dir</span> = <span style="color: #800080;">$this</span>->groups[<span style="color: #800080;">$group</span>]['home'<span style="color: #000000;">];

        }

        </span><span style="color: #800080;">$dir</span> = !<span style="color: #0000ff;">empty</span>(<span style="color: #800080;">$this</span>->users[<span style="color: #800080;">$user</span>]['home'])?<span style="color: #800080;">$this</span>->users[<span style="color: #800080;">$user</span>]['home']:<span style="color: #800080;">$dir</span><span style="color: #000000;">;

        </span><span style="color: #0000ff;">return</span> <span style="color: #800080;">$dir</span><span style="color: #000000;">;

    }

     

    </span><span style="color: #008000;">//</span><span style="color: #008000;">文件权限判断</span>

    <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span> isReadable(<span style="color: #800080;">$user</span>,<span style="color: #800080;">$path</span><span style="color: #000000;">){       

        </span><span style="color: #800080;">$result</span> = <span style="color: #800080;">$this</span>->getPathAccess(<span style="color: #800080;">$user</span>, <span style="color: #800080;">$path</span><span style="color: #000000;">);

        </span><span style="color: #0000ff;">if</span>(<span style="color: #800080;">$result</span>['isExactMatch'<span style="color: #000000;">]){

            </span><span style="color: #0000ff;">return</span> <span style="color: #800080;">$result</span>['access'][0] == 'R'<span style="color: #000000;">;

        }</span><span style="color: #0000ff;">else</span><span style="color: #000000;">{

            </span><span style="color: #0000ff;">return</span> <span style="color: #800080;">$result</span>['access'][0] == 'R' && <span style="color: #800080;">$result</span>['access'][9] == 'I'<span style="color: #000000;">;

        }

    }   

     

    </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span> isWritable(<span style="color: #800080;">$user</span>,<span style="color: #800080;">$path</span><span style="color: #000000;">){       

        </span><span style="color: #800080;">$result</span> = <span style="color: #800080;">$this</span>->getPathAccess(<span style="color: #800080;">$user</span>, <span style="color: #800080;">$path</span><span style="color: #000000;">);       

        </span><span style="color: #0000ff;">if</span>(<span style="color: #800080;">$result</span>['isExactMatch'<span style="color: #000000;">]){

            </span><span style="color: #0000ff;">return</span> <span style="color: #800080;">$result</span>['access'][1] == 'W'<span style="color: #000000;">;

        }</span><span style="color: #0000ff;">else</span><span style="color: #000000;">{

            </span><span style="color: #0000ff;">return</span> <span style="color: #800080;">$result</span>['access'][1] == 'W' && <span style="color: #800080;">$result</span>['access'][9] == 'I'<span style="color: #000000;">;

        }

    }

     

    </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span> isAppendable(<span style="color: #800080;">$user</span>,<span style="color: #800080;">$path</span><span style="color: #000000;">){

        </span><span style="color: #800080;">$result</span> = <span style="color: #800080;">$this</span>->getPathAccess(<span style="color: #800080;">$user</span>, <span style="color: #800080;">$path</span><span style="color: #000000;">);

        </span><span style="color: #0000ff;">if</span>(<span style="color: #800080;">$result</span>['isExactMatch'<span style="color: #000000;">]){

            </span><span style="color: #0000ff;">return</span> <span style="color: #800080;">$result</span>['access'][2] == 'A'<span style="color: #000000;">;

        }</span><span style="color: #0000ff;">else</span><span style="color: #000000;">{

            </span><span style="color: #0000ff;">return</span> <span style="color: #800080;">$result</span>['access'][2] == 'A' && <span style="color: #800080;">$result</span>['access'][9] == 'I'<span style="color: #000000;">;

        }

    }   

     

    </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span> isRenamable(<span style="color: #800080;">$user</span>,<span style="color: #800080;">$path</span><span style="color: #000000;">){

        </span><span style="color: #800080;">$result</span> = <span style="color: #800080;">$this</span>->getPathAccess(<span style="color: #800080;">$user</span>, <span style="color: #800080;">$path</span><span style="color: #000000;">);

        </span><span style="color: #0000ff;">if</span>(<span style="color: #800080;">$result</span>['isExactMatch'<span style="color: #000000;">]){

            </span><span style="color: #0000ff;">return</span> <span style="color: #800080;">$result</span>['access'][3] == 'N'<span style="color: #000000;">;

        }</span><span style="color: #0000ff;">else</span><span style="color: #000000;">{

            </span><span style="color: #0000ff;">return</span> <span style="color: #800080;">$result</span>['access'][3] == 'N' && <span style="color: #800080;">$result</span>['access'][9] == 'I'<span style="color: #000000;">;

        }

    }

    </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span> isDeletable(<span style="color: #800080;">$user</span>,<span style="color: #800080;">$path</span><span style="color: #000000;">){       

        </span><span style="color: #800080;">$result</span> = <span style="color: #800080;">$this</span>->getPathAccess(<span style="color: #800080;">$user</span>, <span style="color: #800080;">$path</span><span style="color: #000000;">);

        </span><span style="color: #0000ff;">if</span>(<span style="color: #800080;">$result</span>['isExactMatch'<span style="color: #000000;">]){

            </span><span style="color: #0000ff;">return</span> <span style="color: #800080;">$result</span>['access'][4] == 'D'<span style="color: #000000;">;

        }</span><span style="color: #0000ff;">else</span><span style="color: #000000;">{

            </span><span style="color: #0000ff;">return</span> <span style="color: #800080;">$result</span>['access'][4] == 'D' && <span style="color: #800080;">$result</span>['access'][9] == 'I'<span style="color: #000000;">;

        }

    }

     

    </span><span style="color: #008000;">//</span><span style="color: #008000;">目录权限判断</span>

    <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span> isFolderListable(<span style="color: #800080;">$user</span>,<span style="color: #800080;">$path</span><span style="color: #000000;">){

        </span><span style="color: #800080;">$result</span> = <span style="color: #800080;">$this</span>->getPathAccess(<span style="color: #800080;">$user</span>, <span style="color: #800080;">$path</span><span style="color: #000000;">);

        </span><span style="color: #0000ff;">if</span>(<span style="color: #800080;">$result</span>['isExactMatch'<span style="color: #000000;">]){

            </span><span style="color: #0000ff;">return</span> <span style="color: #800080;">$result</span>['access'][5] == 'L'<span style="color: #000000;">;

        }</span><span style="color: #0000ff;">else</span><span style="color: #000000;">{

            </span><span style="color: #0000ff;">return</span> <span style="color: #800080;">$result</span>['access'][5] == 'L' && <span style="color: #800080;">$result</span>['access'][9] == 'I'<span style="color: #000000;">;

        }

    }

     

    </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span> isFolderCreatable(<span style="color: #800080;">$user</span>,<span style="color: #800080;">$path</span><span style="color: #000000;">){

        </span><span style="color: #800080;">$result</span> = <span style="color: #800080;">$this</span>->getPathAccess(<span style="color: #800080;">$user</span>, <span style="color: #800080;">$path</span><span style="color: #000000;">);

        </span><span style="color: #0000ff;">if</span>(<span style="color: #800080;">$result</span>['isExactMatch'<span style="color: #000000;">]){

            </span><span style="color: #0000ff;">return</span>  <span style="color: #800080;">$result</span>['access'][6] == 'C'<span style="color: #000000;">;

        }</span><span style="color: #0000ff;">else</span><span style="color: #000000;">{

            </span><span style="color: #0000ff;">return</span>  <span style="color: #800080;">$result</span>['access'][6] == 'C' && <span style="color: #800080;">$result</span>['access'][9] == 'I'<span style="color: #000000;">;

        }

    }

     

    </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span> isFolderRenamable(<span style="color: #800080;">$user</span>,<span style="color: #800080;">$path</span><span style="color: #000000;">){

        </span><span style="color: #800080;">$result</span> = <span style="color: #800080;">$this</span>->getPathAccess(<span style="color: #800080;">$user</span>, <span style="color: #800080;">$path</span><span style="color: #000000;">);

        </span><span style="color: #0000ff;">if</span>(<span style="color: #800080;">$result</span>['isExactMatch'<span style="color: #000000;">]){

            </span><span style="color: #0000ff;">return</span> <span style="color: #800080;">$result</span>['access'][7] == 'N'<span style="color: #000000;">;

        }</span><span style="color: #0000ff;">else</span><span style="color: #000000;">{

            </span><span style="color: #0000ff;">return</span> <span style="color: #800080;">$result</span>['access'][7] == 'N' && <span style="color: #800080;">$result</span>['access'][9] == 'I'<span style="color: #000000;">;

        }

    }

     

    </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span> isFolderDeletable(<span style="color: #800080;">$user</span>,<span style="color: #800080;">$path</span><span style="color: #000000;">){

        </span><span style="color: #800080;">$result</span> = <span style="color: #800080;">$this</span>->getPathAccess(<span style="color: #800080;">$user</span>, <span style="color: #800080;">$path</span><span style="color: #000000;">);

        </span><span style="color: #0000ff;">if</span>(<span style="color: #800080;">$result</span>['isExactMatch'<span style="color: #000000;">]){

            </span><span style="color: #0000ff;">return</span> <span style="color: #800080;">$result</span>['access'][8] == 'D'<span style="color: #000000;">;

        }</span><span style="color: #0000ff;">else</span><span style="color: #000000;">{

            </span><span style="color: #0000ff;">return</span> <span style="color: #800080;">$result</span>['access'][8] == 'D' && <span style="color: #800080;">$result</span>['access'][9] == 'I'<span style="color: #000000;">;

        }

    }

     

    </span><span style="color: #008000;">/*</span><span style="color: #008000;">*

     * 获取目录权限

     * @param string $user

     * @param string $path

     * @return array

     * 进行最长路径匹配

     *

     * 返回:

     * array(

     * 'access'=>目前权限   

     *    ,'isExactMatch'=>是否精确匹配

     *   

     * );

     *

     * 如果精确匹配,则忽略inherit.

     * 否则应判断是否继承父目录的权限,

     * 权限位表:

     * +---+---+---+---+---+---+---+---+---+---+

     * | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |

     * +---+---+---+---+---+---+---+---+---+---+

     * | R | W | A | N | D | L | C | N | D | I |

     * +---+---+---+---+---+---+---+---+---+---+

     * |       FILE        |     FOLDER        |

     * +-------------------+-------------------+

     </span><span style="color: #008000;">*/</span>

     

    <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span> getPathAccess(<span style="color: #800080;">$user</span>,<span style="color: #800080;">$path</span><span style="color: #000000;">){

        </span><span style="color: #800080;">$this</span>-><span style="color: #000000;">reload();

        </span><span style="color: #800080;">$user</span> = <span style="color: #008080;">strtolower</span>(<span style="color: #800080;">$user</span><span style="color: #000000;">);

        </span><span style="color: #800080;">$group</span> = <span style="color: #800080;">$this</span>->users[<span style="color: #800080;">$user</span>]['group'<span style="color: #000000;">];       

        </span><span style="color: #008000;">//</span><span style="color: #008000;">去除文件名称</span>

        <span style="color: #800080;">$path</span> = <span style="color: #008080;">str_replace</span>(<span style="color: #008080;">substr</span>(<span style="color: #008080;">strrchr</span>(<span style="color: #800080;">$path</span>, '/'),1),'',<span style="color: #800080;">$path</span><span style="color: #000000;">);

        </span><span style="color: #800080;">$access</span> = self::AC(0<span style="color: #000000;">);       

        </span><span style="color: #800080;">$isExactMatch</span> = <span style="color: #0000ff;">false</span><span style="color: #000000;">;

        </span><span style="color: #0000ff;">if</span>(<span style="color: #800080;">$group</span><span style="color: #000000;">){

            </span><span style="color: #0000ff;">if</span>(<span style="color: #0000ff;">isset</span>(<span style="color: #800080;">$this</span>->groups[<span style="color: #800080;">$group</span>]['folder'<span style="color: #000000;">])){               

                </span><span style="color: #0000ff;">foreach</span> (<span style="color: #800080;">$this</span>->groups[<span style="color: #800080;">$group</span>]['folder'] <span style="color: #0000ff;">as</span> <span style="color: #800080;">$f</span><span style="color: #000000;">){

                    </span><span style="color: #008000;">//</span><span style="color: #008000;">中文处理</span>

                    <span style="color: #800080;">$t_path</span> = <span style="color: #008080;">iconv</span>('UTF-8','GB18030',<span style="color: #800080;">$f</span>['path'<span style="color: #000000;">]);                   

                    </span><span style="color: #0000ff;">if</span>(<span style="color: #008080;">strpos</span>(<span style="color: #800080;">$path</span>, <span style="color: #800080;">$t_path</span>) === 0<span style="color: #000000;">){

                        </span><span style="color: #800080;">$access</span> = <span style="color: #800080;">$f</span>['access'<span style="color: #000000;">];                       

                        </span><span style="color: #800080;">$isExactMatch</span> = (<span style="color: #800080;">$path</span> == <span style="color: #800080;">$t_path</span>?<span style="color: #0000ff;">true</span>:<span style="color: #0000ff;">false</span><span style="color: #000000;">);

                    }                       

   </span>

로그인 후 복사
원천:php.cn
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 추천
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿