PageRenderTime 259ms CodeModel.GetById 117ms RepoModel.GetById 11ms app.codeStats 0ms

/core/inc/class/class.cpanel.php

https://bitbucket.org/greenbox/microframework
PHP | 1182 lines | 639 code | 74 blank | 469 comment | 53 complexity | c67c9c6e534707fcaabdca5d5399bd25 MD5 | raw file
Possible License(s): LGPL-2.1, GPL-3.0
  1. <?php
  2. /*
  3. **********************************************
  4. **********************************************
  5. ***PHP cPanel API ***
  6. ***Copyright Brendan Donahue, 2006 ***
  7. **********************************************
  8. ***Feature List: ***
  9. *** Connect to cPanel via HTTP or SSL ***
  10. *** List bandwidth and disk space usage ***
  11. *** Change contact settings/passwords ***
  12. *** List, create, modify, and delete: ***
  13. *** Databases and MySQL users ***
  14. *** FTP and email accounts, quotas ***
  15. *** Parked, addon, and subdomains ***
  16. *** Apache redirects ***
  17. *** Email autoresponders ***
  18. *** Forwarders and default addresses***
  19. **********************************************
  20. **********************************************
  21. */
  22. /**
  23. * @ignore
  24. */
  25. class HTTP
  26. {
  27. function HTTP($host, $username, $password, $port = 2082, $ssl = '', $theme = 'x')
  28. {
  29. $this->ssl = $ssl ? 'ssl://' : '';
  30. $this->username = $username;
  31. $this->password = $password;
  32. $this->theme = $theme;
  33. $this->auth = base64_encode($username . ':' . $password);
  34. $this->port = $port;
  35. $this->host = $host;
  36. $this->path = '/frontend/' . $theme . '/';
  37. }
  38. function getData($url, $data = '')
  39. {
  40. $url = $this->path . $url;
  41. if(is_array($data))
  42. {
  43. $url = $url . '?';
  44. foreach($data as $key=>$value)
  45. {
  46. $url .= urlencode($key) . '=' . urlencode($value) . '&';
  47. }
  48. $url = substr($url, 0, -1);
  49. }
  50. $response = '';
  51. $fp = fsockopen($this->ssl . $this->host, $this->port);
  52. if(!$fp)
  53. {
  54. return false;
  55. }
  56. $out = 'GET ' . $url . ' HTTP/1.0' . "\r\n";
  57. $out .= 'Authorization: Basic ' . $this->auth . "\r\n";
  58. $out .= 'Connection: Close' . "\r\n\r\n";
  59. fwrite($fp, $out);
  60. while (!feof($fp))
  61. {
  62. $response .= @fgets($fp);
  63. }
  64. fclose($fp);
  65. return $response;
  66. }
  67. }
  68. /**
  69. * Functions to manipulate cPanel
  70. */
  71. class cPanel
  72. {
  73. /**
  74. * Creates an object to manipulate cPanel
  75. * @param string $host cPanel host without leading http://
  76. * @param string $username cPanel username
  77. * @param string $password cPanel password
  78. * @param int $port cPanel port, default to 2082. Change to 2083 if using SSL
  79. * @param bool $ssl False for http (default), true for SSL (requires OpenSSL)
  80. * @param string $theme cPanel theme, (forward compatibility- 'x' theme currently required)
  81. * @return cPanel
  82. */
  83. function cPanel($host, $username, $password, $port = 2082, $ssl = false, $theme = 'x')
  84. {
  85. $this->HTTP = new HTTP($host, $username, $password, $port, $ssl, $theme);
  86. }
  87. /**
  88. * Change cPanel's password
  89. *
  90. * Returns true on success or false on failure.
  91. * The cPanel object is no longer usable after changing the password.
  92. * @param string $password new password
  93. * @return bool
  94. */
  95. function setPassword($password)
  96. {
  97. $data['oldpass'] = $this->HTTP->password;
  98. $data['newpass'] = $password;
  99. $response = $this->HTTP->getData('passwd/changepass.html', $data);
  100. if(strpos($response, 'has been') && !strpos($response, 'could not'))
  101. {
  102. return true;
  103. }
  104. return false;
  105. }
  106. /**
  107. * Retrieve contact email address.
  108. *
  109. * Returns the contact email address listed in cPanel.
  110. * @return string
  111. */
  112. function getContactEmail()
  113. {
  114. $email = array();
  115. preg_match('/email" value="(.*)"/', $this->HTTP->getData('contact/index.html'), $email);
  116. return $email[1];
  117. }
  118. /**
  119. * Modify contact email address
  120. *
  121. * Returns true on success or false on failure.
  122. * @param string new contact email address
  123. * @return string
  124. */
  125. function setContactEmail($email)
  126. {
  127. $data['email'] = $email;
  128. $response = $this->HTTP->getData('contact/saveemail.html', $data);
  129. if(strpos($response, 'has been'))
  130. {
  131. return true;
  132. }
  133. return false;
  134. }
  135. /**
  136. * List all domains in the cPanel account
  137. *
  138. * Returns a numerically-indexed array on success or false on failure.
  139. * @return array
  140. */
  141. function listDomains()
  142. {
  143. $domainList = array();
  144. preg_match_all('/<option value="([^"]*)/', $this->HTTP->getData('mail/addpop2.html'), $domainList);
  145. if(count($domainList[1]) > 0)
  146. {
  147. return $domainList[1];
  148. }
  149. return false;
  150. }
  151. /**
  152. * List all POP3 email accounts
  153. *
  154. * Returns a numerically-indexed array on success or false on failure.
  155. * @return array
  156. */
  157. function listMailAccounts()
  158. {
  159. $accountList = array();
  160. preg_match_all('/\?acct=([^"]*)/', $this->HTTP->getData('mail/pops.html'), $accountList);
  161. if(count($accountList[1]) > 0)
  162. {
  163. return $accountList[1];
  164. }
  165. return false;
  166. }
  167. /**
  168. * List MySQL database users
  169. *
  170. * Returns a numerically-indexed array on success. Returns an empty array if no users exist.
  171. * @return array
  172. */
  173. function listDBUsers()
  174. {
  175. $accountList = array();
  176. preg_match_all('/\?user=([^"]*)/', $this->HTTP->getData('sql/index.html'), $accountList);
  177. return $accountList[1];
  178. }
  179. /**
  180. * List MySQL databases
  181. *
  182. * Returns a numerically-indexed array on success. Returns an empty array if no databases exist.
  183. * @return array
  184. */
  185. function listDatabases()
  186. {
  187. $databaseList = array();
  188. preg_match_all('/deldb.html\?db=([^"]*)/', $this->HTTP->getData('sql/index.html'), $databaseList);
  189. return $databaseList[1];
  190. }
  191. /**
  192. * List FTP accounts
  193. *
  194. * Returns a numerically-indexed array on success or false on failure. This function does not include accounts listed as "Main Account".
  195. * @return array
  196. */
  197. function listFTPAccounts()
  198. {
  199. $accountList = Array();
  200. preg_match_all('/passwdftp.html\?acct=([^"]*)/', $this->HTTP->getData('ftp/accounts.html'), $accountList);
  201. return array_unique($accountList[1]);
  202. }
  203. /**
  204. * List parked domains
  205. *
  206. * Returns a numerically-indexed array on success. Returns an empty array if no domains are parked.
  207. * @return array
  208. */
  209. function listParked()
  210. {
  211. $domainList = array();
  212. preg_match_all('/<option value="([^"]*)/', $this->HTTP->getData('park/index.html'), $domainList);
  213. return $domainList[1];
  214. }
  215. /**
  216. * List addon domains
  217. *
  218. * Returns a numerically-indexed array of comma-delimited values on success. Returns an empty array if no addon domains exist.
  219. * @return array
  220. */
  221. function listAddons()
  222. {
  223. $domainList = array();
  224. $data = explode('Remove Addon', $this->HTTP->getData('addon/index.html'));
  225. preg_match_all('/<option value="(.*)">(.*)<\/option>/', $data[1], $domainList);
  226. return $domainList[0];
  227. }
  228. /**
  229. * List subdomains
  230. *
  231. * Returns a numerically-indexed array on success. Returns an empty array if no subdomains exist.
  232. * @return array
  233. */
  234. function listSubdomains()
  235. {
  236. $domainList = array();
  237. $domains = explode('</select>', $this->HTTP->getData('subdomain/index.html'));
  238. $domains = explode('</select>', $domains[2]);
  239. preg_match_all('/<option value="(.*)">(.*)<\/option>/', $domains[0], $domainList);
  240. return $domainList[2];
  241. }
  242. /**
  243. * List Apache redirects
  244. *
  245. * These may be permanent or temporary redirects (status codes 301 and 302). Returns a numerically-indexed array on success. Returns an empty array if no redirects exist.
  246. * @return array
  247. */
  248. function listRedirects()
  249. {
  250. $redirectList = array();
  251. preg_match_all('/<option value="\/([^"]*)/', $this->HTTP->getData('mime/redirect.html'), $redirectList);
  252. return $redirectList[1];
  253. }
  254. /**
  255. * Parse account information
  256. *
  257. * Returns General account information or General server information. Used internally by getFreeSpace(), getSpaceUsed(), etc.
  258. * @param string $key key to parse for
  259. * @param string $type type of value to return (int, float, or string)
  260. * @return string
  261. */
  262. function parseIndex($key, $type = 'string')
  263. {
  264. $value = array();
  265. preg_match('/' . $key . '<\/td>' . "\n" . ' <td class="index2">(.*)<\/td>/', $this->HTTP->getData('index.html'), $value);
  266. settype($value[1], $type);
  267. return $value[1];
  268. }
  269. /**
  270. * Get free disk space
  271. *
  272. * Returns the amount of disk space available in megabytes.
  273. * @return mixed
  274. */
  275. function getFreeSpace()
  276. {
  277. $freeSpace = $this->parseIndex('Disk space available', 'float');
  278. return ($freeSpace == 0) ? 'Unlimited' : floatval($freeSpace);
  279. }
  280. /**
  281. * Get used disk space
  282. *
  283. * Returns the amount of disk space used in megabytes.
  284. * @return float
  285. */
  286. function getSpaceUsed()
  287. {
  288. return $this->parseIndex('Disk Space Usage', 'float');
  289. }
  290. /**
  291. * Get MySQL space usage
  292. *
  293. * Returns the amount of disk space used by MySQL databases in megabytes.
  294. * @return float
  295. */
  296. function getMySQLSpaceUsed()
  297. {
  298. return $this->parseIndex('MySQL Disk Space', 'float');
  299. }
  300. /**
  301. * Get bandwidth usage
  302. *
  303. * Returns the amount of bandwidth used this month in megabytes.
  304. * @return float
  305. */
  306. function getBandwidthUsed()
  307. {
  308. return $this->parseIndex('Bandwidth \(this month\)', 'float');
  309. }
  310. /**
  311. * Get hosting package name
  312. * @return string
  313. */
  314. function getHostingPackage()
  315. {
  316. return $this->parseIndex('Hosting package');
  317. }
  318. /**
  319. * Get shared IP address
  320. * @return string
  321. */
  322. function getSharedIP()
  323. {
  324. return $this->parseIndex('Shared Ip Address');
  325. }
  326. /**
  327. * Creates an object to manipulate email account
  328. * @param string $address email address of account to manipulate
  329. * @return emailAccount
  330. */
  331. function openEmailAccount($address)
  332. {
  333. return new emailAccount($this->HTTP->host, $this->HTTP->username, $this->HTTP->password, $this->HTTP->port, $this->HTTP->ssl, $this->HTTP->theme, $address);
  334. }
  335. /**
  336. * Creates an object to manipulate domain
  337. * @param string $domain domain to manipulate
  338. * @return Domain
  339. */
  340. function openDomain($domain)
  341. {
  342. return new Domain($this->HTTP->host, $this->HTTP->username, $this->HTTP->password, $this->HTTP->port, $this->HTTP->ssl, $this->HTTP->theme, $domain);
  343. }
  344. /**
  345. * Creates an object to manipulate FTP account
  346. * @param string name of FTP account to manipulate
  347. * @return FTPAccount
  348. */
  349. function openFTPAccount($account)
  350. {
  351. return new FTPAccount($this->HTTP->host, $this->HTTP->username, $this->HTTP->password, $this->HTTP->port, $this->HTTP->ssl, $this->HTTP->theme, $account);
  352. }
  353. /**
  354. * Creates an object to manipulate database
  355. * @param string $database name of MySQL database to manipulate
  356. * @return Database
  357. */
  358. function openDatabase($database)
  359. {
  360. return new Database($this->HTTP->host, $this->HTTP->username, $this->HTTP->password, $this->HTTP->port, $this->HTTP->ssl, $this->HTTP->theme, $database);
  361. }
  362. /**
  363. * Creates an object to manipulate database user
  364. * @param string $user username of MySQL user to manipulate
  365. * @return databaseUser
  366. */
  367. function openDatabaseUser($user)
  368. {
  369. return new databaseUser($this->HTTP->host, $this->HTTP->username, $this->HTTP->password, $this->HTTP->port, $this->HTTP->ssl, $this->HTTP->theme, $user);
  370. }
  371. /**
  372. * Creates an object to manipulate redirect
  373. * @param string $path server path to manipulate redirection on
  374. * @return Redirect
  375. */
  376. function openRedirect($path)
  377. {
  378. return new Redirect($this->HTTP->host, $this->HTTP->username, $this->HTTP->password, $this->HTTP->port, $this->HTTP->ssl, $this->HTTP->theme, $path);
  379. }
  380. }
  381. /**
  382. * Functions to manipulate cPanel email accounts
  383. */
  384. class emailAccount
  385. {
  386. /**
  387. * @ignore
  388. */
  389. function emailAccount($host, $username, $password, $port = 2082, $ssl = false, $theme = 'x', $address)
  390. {
  391. $this->HTTP = new HTTP($host, $username, $password, $port, $ssl, $theme);
  392. if(strpos($address, '@'))
  393. {
  394. list($this->email, $this->domain) = explode('@', $address);
  395. }
  396. else
  397. {
  398. list($this->email, $this->domain) = array($address, '');
  399. }
  400. }
  401. /**
  402. * Create email account in cPanel
  403. *
  404. * Returns true on success or false on failure.
  405. * @param string $password email account password
  406. * @param int $quota quota for email account in megabytes
  407. * @return bool
  408. */
  409. function create($password, $quota)
  410. {
  411. $data['email'] = $this->email;
  412. $data['domain'] = $this->domain;
  413. $data['password'] = $password;
  414. $data['quota'] = $quota;
  415. $response = $this->HTTP->getData('mail/doaddpop.html', $data);
  416. if(strpos($response, 'failure') || strpos($response, 'already exists'))
  417. {
  418. return false;
  419. }
  420. return true;
  421. }
  422. /**
  423. * Get space used by account
  424. *
  425. * Returns the amount of disk space used by email account in megabytes.
  426. * @return int
  427. */
  428. function getUsedSpace()
  429. {
  430. $usedSpace = array();
  431. preg_match('/' . $this->email . '@' . $this->domain . "<\\/font><\\/td>\n <td align=\"center\" valign=\"top\">([^&]*)/", $this->HTTP->getData('mail/pops.html?extras=disk'), $usedSpace);
  432. return $usedSpace[1];
  433. }
  434. /**
  435. * Get account storage quota
  436. *
  437. * Returns amount of disk space allowed for email account in megabytes.
  438. * @return int
  439. */
  440. function getQuota()
  441. {
  442. $quota = array();
  443. $data['email'] = $this->email;
  444. $data['domain'] = $this->domain;
  445. preg_match('/quota" value="([^"]*)/', $this->HTTP->getData('mail/editquota.html', $data), $quota);
  446. return ($quota[1] == 0) ? 'Unlimited' : intval($quota[1]);
  447. }
  448. /**
  449. * Modify account storage quota
  450. *
  451. * Returns true on success or false on failure.
  452. * @param int $quota quota for email account in megabytes
  453. * @return bool
  454. */
  455. function setQuota($quota)
  456. {
  457. $data['email'] = $this->email;
  458. $data['domain'] = $this->domain;
  459. $data['quota'] = $quota;
  460. $response = $this->HTTP->getData('mail/doeditquota.html', $data);
  461. if(strpos($response, 'success'))
  462. {
  463. return true;
  464. }
  465. return false;
  466. }
  467. /**
  468. * Change email account password
  469. *
  470. * Returns true on success or false on failure.
  471. * @param string $password email account password
  472. * @return bool
  473. */
  474. function setPassword($password)
  475. {
  476. $data['email'] = $this->email;
  477. $data['domain'] = $this->domain;
  478. $data['password'] = $password;
  479. $response = $this->HTTP->getData('mail/dopasswdpop.html', $data);
  480. if(strpos($response, 'success') && !strpos($response, 'failure'))
  481. {
  482. return true;
  483. }
  484. return false;
  485. }
  486. /**
  487. * List email forwarders
  488. *
  489. * Returns a numerically-indexed array of forwarders for the email account. Returns an empty array if there are no forwarders.
  490. * @return array
  491. */
  492. function listForwarders()
  493. {
  494. $forwarders = array();
  495. preg_match_all('/\?email=' . $this->email . '@' . $this->domain . '=([^"]*)/', $this->HTTP->getData('mail/fwds.html'), $forwarders);
  496. return $forwarders[1];
  497. }
  498. /**
  499. * Create email forwarder
  500. *
  501. * Returns true on success or false on failure.
  502. * @param string $forward forwarding address
  503. * @return bool
  504. */
  505. function addForwarder($forward)
  506. {
  507. $data['email'] = $this->email;
  508. $data['domain'] = $this->domain;
  509. $data['forward'] = $forward;
  510. $response = $this->HTTP->getData('mail/doaddfwd.html', $data);
  511. if(strpos($response, 'redirected'))
  512. {
  513. return true;
  514. }
  515. return false;
  516. }
  517. function addForwarderSpecial($forward , $mail , $domain)
  518. {
  519. $data['email'] = $mail;
  520. $data['domain'] = $domain;
  521. $data['forward'] = $forward;
  522. $response = $this->HTTP->getData('mail/doaddfwd.html', $data);
  523. if(strpos($response, 'redirected'))
  524. {
  525. return true;
  526. }
  527. return false;
  528. }
  529. /**
  530. * Delete email forwarder
  531. *
  532. * Permanently removes the account's email forwarder and returns true.
  533. * @param string $forwarder forwarding address to delete
  534. * @return bool
  535. */
  536. function delForwarder($forwarder)
  537. {
  538. $data['email'] = $this->email . '@' . $this->domain . '=' . $forwarder;
  539. $this->HTTP->getData('mail/dodelfwd.html', $data);
  540. return true;
  541. }
  542. /**
  543. * Create email autoresponder
  544. *
  545. * Returns true on success or false on failure.
  546. * @param string $from from email address
  547. * @param string $subject email subject line
  548. * @param string $charset character set
  549. * @param bool $html true for HTML email
  550. * @param string $body body of email message
  551. * @return bool
  552. */
  553. function addAutoResponder($from, $subject, $charset, $html, $body)
  554. {
  555. $data['email'] = $this->email;
  556. $data['domain'] = $this->domain;
  557. $data['from'] = $from;
  558. $data['subject'] = $subject;
  559. $data['charset'] = $charset;
  560. if($html)
  561. {
  562. $data['html'] = $html;
  563. }
  564. $data['body'] = $body;
  565. $response = $this->HTTP->getData('mail/doaddars.html', $data);
  566. if(strpos($response, 'success') && !strpos($response, 'failure'))
  567. {
  568. return true;
  569. }
  570. return false;
  571. }
  572. /**
  573. * Delete email autoresponder
  574. *
  575. * Deletes autoresponder for email account if it exists, and returns true.
  576. * @return bool
  577. */
  578. function delAutoResponder()
  579. {
  580. $this->HTTP->getData('mail/dodelautores.html?email=' . $this->email . '@' . $this->domain);
  581. return true;
  582. }
  583. /**
  584. * Delete email account
  585. *
  586. * Permanenetly removes POP3 account. Returns true on success or false on failure.
  587. * @return bool
  588. */
  589. function delete()
  590. {
  591. $data['email'] = $this->email;
  592. $data['domain'] = $this->domain;
  593. $response = $this->HTTP->getData('mail/realdelpop.html', $data);
  594. if(strpos($response, 'success'))
  595. {
  596. return true;
  597. }
  598. return false;
  599. }
  600. }
  601. /**
  602. * Functions to manipulate domains in cPanel
  603. */
  604. class Domain
  605. {
  606. /**
  607. * @ignore
  608. */
  609. function Domain($host, $username, $password, $port = 2082, $ssl = false, $theme = 'x', $domain)
  610. {
  611. $this->HTTP = new HTTP($host, $username, $password, $port, $ssl, $theme);
  612. $this->domain = $domain;
  613. }
  614. /**
  615. * Get default address
  616. *
  617. * Retrieves the default email address for the domain.
  618. * @return string
  619. */
  620. function getDefaultAddress()
  621. {
  622. $default = explode('<b>' . $this->domain . '</b>', $this->HTTP->getData('mail/def.html'));
  623. if($default[1])
  624. {
  625. $default = explode('<td>', $default[1]);
  626. $default = explode('</td>', $default[1]);
  627. return trim($default[0]);
  628. }
  629. }
  630. /**
  631. * Modify default address
  632. *
  633. * Changes the default email address for the domain. Returns true on success or false on failure.
  634. * @param string $adderss new default address
  635. * @return bool
  636. */
  637. function setDefaultAddress($address)
  638. {
  639. $data['domain'] = $this->domain;
  640. $data['forward'] = $address;
  641. $response = $this->HTTP->getData('mail/dosetdef.html', $data);
  642. if(strpos($response, 'is now'))
  643. {
  644. return true;
  645. }
  646. return false;
  647. }
  648. /**
  649. * Park domain
  650. *
  651. * Returns true on success or false on failure.
  652. * @return bool
  653. */
  654. function parkDomain()
  655. {
  656. $data['domain'] = $this->domain;
  657. $response = $this->HTTP->getData('park/doaddparked.html', $data);
  658. if(strpos($response, 'success') && !strpos($response, 'error'))
  659. {
  660. return true;
  661. }
  662. return false;
  663. }
  664. /**
  665. * Delete parked domain
  666. *
  667. * Returns true on success or false on failure.
  668. * @return bool
  669. */
  670. function unparkDomain()
  671. {
  672. $data['domain'] = $this->domain;
  673. $response = $this->HTTP->getData('park/dodelparked.html', $data);
  674. if(strpos($response, 'success') && !strpos($response, 'Error'))
  675. {
  676. return true;
  677. }
  678. return false;
  679. }
  680. /**
  681. * Create addon domain
  682. *
  683. * Returns true on success or false on failure.
  684. * @param string $user username or directory
  685. * @param string $pass password
  686. * @return bool
  687. */
  688. function addonDomain($user, $pass)
  689. {
  690. $data['domain'] = $this->domain;
  691. $data['user'] = $user;
  692. $data['pass'] = $pass;
  693. $response = $this->HTTP->getData('addon/doadddomain.html', $data);
  694. if(strpos($response, 'added') && !strpos($response, 'Error'))
  695. {
  696. return true;
  697. }
  698. return false;
  699. }
  700. /**
  701. * Delete addon domain
  702. *
  703. * Returns true on success or false on failure.
  704. * @return bool
  705. */
  706. function delAddonDomain()
  707. {
  708. $data['domain'] = $this->domain;
  709. $response = $this->HTTP->getData('addon/dodeldomain.html', $data);
  710. if(strpos($response, 'success') && !strpos($response, 'Error'))
  711. {
  712. return true;
  713. }
  714. return false;
  715. }
  716. /**
  717. * Create subdomain
  718. *
  719. * Returns true on success or false on failure.
  720. * @param string $subdomain name of subdomain to create
  721. * @return bool
  722. */
  723. function addSubdomain($subdomain)
  724. {
  725. $data['domain'] = $subdomain;
  726. $data['rootdomain'] = $this->domain;
  727. $response = $this->HTTP->getData('subdomain/doadddomain.html', $data);
  728. if(strpos($response, 'added') && !strpos($response, 'Error'))
  729. {
  730. return true;
  731. }
  732. return false;
  733. }
  734. /**
  735. * Get subdomain redirection
  736. *
  737. * Returns the URL a subdomain is redirected to.
  738. * @return string
  739. */
  740. function getSubdomainRedirect($subdomain)
  741. {
  742. $redirect = array();
  743. $data['domain'] = $subdomain . '_' . $this->domain;
  744. preg_match('/40 value="([^"]*)/', $this->HTTP->getData('subdomain/doredirectdomain.html', $data), $redirect);
  745. return $redirect[1];
  746. }
  747. /**
  748. * Redirect subdomain
  749. *
  750. * Redirects a subdomain of the current domain to another address.
  751. * @param string $subdomain name of subdomain
  752. * @param string $url url to redirect to
  753. * @return bool
  754. */
  755. function redirectSubdomain($subdomain, $url)
  756. {
  757. $data['domain'] = $subdomain . '_' . $this->domain;
  758. $data['url'] = $url;
  759. $response = $this->HTTP->getData('subdomain/saveredirect.html', $data);
  760. if(strpos($response, 'redirected') && !strpos($response, 'Disabled'))
  761. {
  762. return true;
  763. }
  764. return false;
  765. }
  766. /**
  767. * Remove subdomain redirection
  768. *
  769. * @param string $subdomain name of subdomain
  770. * @return bool
  771. */
  772. function delRedirectSubdomain($subdomain)
  773. {
  774. $data['domain'] = $subdomain . '_' . $this->domain;
  775. $response = $this->HTTP->getData('subdomain/donoredirect.html', $data);
  776. if(strpos($response, 'disabled'))
  777. {
  778. return true;
  779. }
  780. return false;
  781. }
  782. /**
  783. * Delete subdomain
  784. *
  785. * Returns true on success or false on failure.
  786. * @param string $subdomain name of subdomain to delete
  787. * @return bool
  788. */
  789. function delSubdomain($subdomain)
  790. {
  791. $data['domain'] = $subdomain . '_' . $this->domain;
  792. $response = $this->HTTP->getData('subdomain/dodeldomain.html', $data);
  793. if(strpos($response, 'Removed'))
  794. {
  795. return true;
  796. }
  797. return false;
  798. }
  799. }
  800. /**
  801. * Functions to manipulate cPanel FTP accounts
  802. */
  803. class FTPAccount
  804. {
  805. /**
  806. * Delete email autoresponder
  807. * @ignore
  808. */
  809. function FTPAccount($host, $username, $password, $port = 2082, $ssl = false, $theme = 'x', $account)
  810. {
  811. $this->HTTP = new HTTP($host, $username, $password, $port, $ssl, $theme);
  812. $this->account = $account;
  813. }
  814. /**
  815. * Create FTP account
  816. *
  817. * Returns true on success or false on failure.
  818. * @param string $password account password
  819. * @param string $quota disk space quota in megabytes
  820. * @param string directory user's home directory
  821. * @return bool
  822. */
  823. function create($password, $quota, $directory)
  824. {
  825. $data['login'] = $this->account;
  826. $data['password'] = $password;
  827. $data['quota'] = $quota;
  828. $data['homedir'] = $directory;
  829. $response = $this->HTTP->getData('ftp/doaddftp.html', $data);
  830. if(strpos($response, 'failure') || strpos($response, 'Fatal') || !strpos($response, 'Added'))
  831. {
  832. return false;
  833. }
  834. return true;
  835. }
  836. /**
  837. * Get used space
  838. *
  839. * Returns the amount of disk space used by the FTP account.
  840. * @return int
  841. */
  842. function getUsedSpace()
  843. {
  844. $usedSpace = explode('<td>' . $this->account . '</td>', $this->HTTP->getData('ftp/accounts.html'));
  845. $usedSpace = explode('</td><td>', $usedSpace[1], 2);
  846. return floatval(substr($usedSpace[1], 0, strpos($usedSpace[1], '/')));
  847. }
  848. /**
  849. * Get storage quota
  850. *
  851. * Returns the storage quota of the FTP account in megabytes.
  852. * @return bool
  853. */
  854. function getQuota()
  855. {
  856. $quota = array();
  857. $data['acct'] = $this->account;
  858. preg_match('/"quota" value="([^"]*)/', $this->HTTP->getData('ftp/editquota.html', $data), $quota);
  859. return ($quota[1] == 0) ? 'Unlimited' : intval($quota[1]);
  860. }
  861. /**
  862. * Set storage quota
  863. *
  864. * Modifies the maximum disk space allowed for the FTP account. Returns true on success or false on failure.
  865. * @param int $quota new quota in megabytes
  866. * @return bool
  867. */
  868. function setQuota($quota)
  869. {
  870. $data['acct'] = $this->account;
  871. $data['quota'] = $quota;
  872. $response = $this->HTTP->getData('ftp/doeditquota.html', $data);
  873. if(strpos($response, 'success'))
  874. {
  875. return true;
  876. }
  877. return false;
  878. }
  879. /**
  880. * Change password
  881. *
  882. * Changes the FTP account password and returns true on success or false on failure.
  883. * @param string $password new password
  884. * @return bool
  885. */
  886. function setPassword($password)
  887. {
  888. $data['acct'] = $this->account;
  889. $data['password'] = $password;
  890. $response = $this->HTTP->getData('ftp/dopasswdftp.html', $data);
  891. if(strpos($response, 'Changed'))
  892. {
  893. return true;
  894. }
  895. return false;
  896. }
  897. /**
  898. * Delete FTP account
  899. *
  900. * Permanently removes the FTP account and returns true on success or false on failure.
  901. * @return bool
  902. */
  903. function delete()
  904. {
  905. $data['login'] = $this->account;
  906. $response = $this->HTTP->getData('ftp/realdodelftp.html', $data);
  907. if(strpos($response, 'deleted'))
  908. {
  909. return true;
  910. }
  911. return false;
  912. }
  913. }
  914. /**
  915. * Functions to manipulate MySQL databases in cPanel
  916. */
  917. class Database
  918. {
  919. /**
  920. * @ignore
  921. */
  922. function Database($host, $username, $password, $port = 2082, $ssl = false, $theme = 'x', $database)
  923. {
  924. $this->HTTP = new HTTP($host, $username, $password, $port, $ssl, $theme);
  925. $this->database = $database;
  926. }
  927. /**
  928. * Create database
  929. *
  930. * Creates a MySQL database with the specified name. Returns true on success or false on failure.
  931. * @return bool
  932. */
  933. function create()
  934. {
  935. $data['db'] = $this->database;
  936. $response = $this->HTTP->getData('sql/adddb.html', $data);
  937. if(strpos($response, 'Added'))
  938. {
  939. return true;
  940. }
  941. return false;
  942. }
  943. /**
  944. * Add user to database
  945. *
  946. * Gives the specified user all permissions on the database and returns true on success or false on failure.
  947. * @param string $user MySQL username to add to database
  948. * @return bool
  949. */
  950. function addUser($user)
  951. {
  952. $data['user'] = $user;
  953. $data['db'] = $this->database;
  954. $data['ALL'] = 'ALL';
  955. $response = $this->HTTP->getData('sql/addusertodb.html', $data);
  956. if(strpos($response, 'Added'))
  957. {
  958. return true;
  959. }
  960. return false;
  961. }
  962. /**
  963. * Delete user from database
  964. *
  965. * Removes the user's permissions from this database.
  966. * @param string $user MySQL username
  967. * @return bool
  968. */
  969. function delUser($user)
  970. {
  971. $data['user'] = $user;
  972. $data['db'] = $this->database;
  973. $response = $this->HTTP->getData('sql/deluserfromdb.html', $data);
  974. if(strpos($response, 'Deleted'))
  975. {
  976. return true;
  977. }
  978. return false;
  979. }
  980. /**
  981. * Delete database
  982. *
  983. * Permanently drops a MySQL database and returns true on success or false on failure.
  984. * @return bool
  985. */
  986. function delete()
  987. {
  988. $data['db'] = $this->database;
  989. $response = $this->HTTP->getData('sql/deldb.html', $data);
  990. if(strpos($response, 'dropped'))
  991. {
  992. return true;
  993. }
  994. return false;
  995. }
  996. }
  997. /**
  998. * Functions to manipulate MySQL database users in cPanel
  999. */
  1000. class databaseUser
  1001. {
  1002. /**
  1003. * @ignore
  1004. */
  1005. function databaseUser($host, $username, $password, $port = 2082, $ssl = false, $theme = 'x', $user)
  1006. {
  1007. $this->HTTP = new HTTP($host, $username, $password, $port, $ssl, $theme);
  1008. $this->user = $user;
  1009. }
  1010. /**
  1011. * Create database user
  1012. *
  1013. * Creates a MySQL user and returns true on success or false on failure.
  1014. * @param string $password MySQL password
  1015. * @return bool
  1016. */
  1017. function create($password)
  1018. {
  1019. $data['user'] = $this->user;
  1020. $data['pass'] = $password;
  1021. $response = $this->HTTP->getData('sql/adduser.html', $data);
  1022. if(strpos($response, 'Added'))
  1023. {
  1024. return true;
  1025. }
  1026. return false;
  1027. }
  1028. /**
  1029. * Delete database user
  1030. *
  1031. * Permenently deletes the MySQL user.
  1032. * @return bool
  1033. */
  1034. function delete()
  1035. {
  1036. $data['user'] = $this->user;
  1037. $response = $this->HTTP->getData('sql/deluser.html', $data);
  1038. if(strpos($response, 'Removed'))
  1039. {
  1040. return true;
  1041. }
  1042. return false;
  1043. }
  1044. }
  1045. /**
  1046. * Functions to manipulate URL redirection in cPanel
  1047. */
  1048. class Redirect
  1049. {
  1050. /**
  1051. * Delete email autoresponder
  1052. * @ignore
  1053. */
  1054. function Redirect($host, $username, $password, $port = 2082, $ssl = false, $theme = 'x', $path)
  1055. {
  1056. $this->HTTP = new HTTP($host, $username, $password, $port, $ssl, $theme);
  1057. $this->path = '/' . $path;
  1058. }
  1059. /**
  1060. * Create redirect
  1061. *
  1062. * Creates a 301 or 302 redirect and returns true on success or false on failure.
  1063. * @param string $url URL to redirect to
  1064. * @param string $type 'permanent' or 'temp'
  1065. * @return bool
  1066. */
  1067. function create($url, $type = 'permanent')
  1068. {
  1069. $data['path'] = $this->path;
  1070. $data['url'] = $url;
  1071. $data['type'] = $type;
  1072. $response = $this->HTTP->getData('mime/addredirect.html', $data);
  1073. if(strpos($response, 'Added'))
  1074. {
  1075. return true;
  1076. }
  1077. return false;
  1078. }
  1079. /**
  1080. * Get redirect URL
  1081. *
  1082. * Get the path a URL is redirected to
  1083. * @return string
  1084. */
  1085. function getRedirectURL()
  1086. {
  1087. $url = array();
  1088. preg_match('%' . $this->path . '</td><td>([^<]*)%', $this->HTTP->getData('mime/redirect.html'), $url);
  1089. return $url[1];
  1090. }
  1091. /**
  1092. * Delete redirect
  1093. *
  1094. * Permanently removes the redirect and returns true on success or false on failure.
  1095. * @return bool
  1096. */
  1097. function delete()
  1098. {
  1099. $data['path'] = '/' . $this->path;
  1100. $response = $this->HTTP->getData('mime/delredirect.html', $data);
  1101. if(strpos($response, 'Removed'))
  1102. {
  1103. return true;
  1104. }
  1105. return false;
  1106. }
  1107. }
  1108. ?>